Jquery summary of three ways to get control values


**Jquery method to get the value of the server control **Because ASP.NET web page runs, the server control will randomly generate the client id, jquery is not easy to get the operation, Google, summed up the following three methods:

Server control code: < Asp: TextBox ID = “txtUserID” runat = “server” > < / asp: TextBox>

1. $(” # < % % = txtUserID. The ClientID >” ). Val ();

2. $(” input [id * = txtUserID] ”). The val ();

3. $(” * $= txtUserID [id] ”). The val ();

Jquery method to get control values

Values:

$("") is a jquery object, not a dom element Value is an attribute of the dom element Jquery corresponds to val Val () : gets the current value of the first matched element. Val (val): sets the value of each matched element. So, the code should read: Value: val = $(“#id”)[0].value; **Assignment: **$(” # id ”) [0]. Value = “new value”; Or $(” # id ”). Val (” new value ”);

Or you can: val = $(“#id”).attr(“value”); Gets the values of a set of selected items of radio

Var item = $(’ input [@ name = items] [@ checked] ’). The val (); Gets the text of the selected item in a select Var item = $(“select[@name=items] option[@selected]“).text(); The second element of the select drop-down box is the currently selected value $(’ # select_id) [0]. SelectedIndex = 1; The second element of the radio radio group is the currently selected value $(’ input [@ name = items] ’). The get (1) checked = true;

Get the value:

Text field: $(”# TXT “).attr(“value”); $(“#checkbox_id”).attr(“value”); Radio set radio: $(” input [@ type = radio] [@ checked] ”). The val (); Select: $(‘#sel’).val();

**Control form elements: **Text field: $(”# TXT “).attr(“value”, ”); // empty the content $(” # TXT ”). Attr (” value ”, “11”); // fill in the content

Multi-checkbox: $(” # chk1 ”). Attr (” checked ”, ”); / / no box $(” # chk2). Attr (” checked ”, true); / / box If ($(” # chk1 ”). The attr (” checked ”) = = undefined) / / determine whether have tick

Radio group: $(” input [@ type = radio] ”). The attr (” checked ”, ‘2’); // the item with value=2 is the currently selected item Drop-down box select: $(” # sel ”). Attr (” value ”, ’- sel3’); // the item with value=-sel3 is the currently selected item $(” < The option value = ‘1’ > 1111 < / option> < The option value = ‘2’ > 2222 < / option>” ). AppendTo (“#sel”)// add options to the drop-down box

$(” # sel ”). The empty (); // empty the drop-down box

**Three     Jquery method to get the value of the control DropDownList **

 <script type="text/javascript">
       function bbOK()
       {        
                var a = $("#ddlGuo option:selected").val();
                var b = $("#ddlGuo option:selected").text();
                $("#txttext").attr("value", b);
                $("#txtval").attr("value", a);        
       }  
    </script>
<html>
  <asp:DropDownList ID="ddlGuo" runat="server"  >
            <asp:ListItem Selected="True" Value="001"> The Beijing municipal </asp:ListItem>
            <asp:ListItem Value="301"> nanjing </asp:ListItem>
            <asp:ListItem Value="313"> suzhou </asp:ListItem>     
        </asp:DropDownList>
        <asp:TextBox ID="txtval" runat="server"></asp:TextBox>
        <asp:TextBox ID="txttext" runat="server"></asp:TextBox
<br />        
            <asp:Button ID="Button1" runat="server" Text=" Click on the Select" OnClientClick="bbOK();" /> 
 </html>