Summary of Jquery's three methods for obtaining control values

Source: Internet
Author: User

A Jquery method for obtaining the Server Control Value
After an ASP. NET web page is running, the server control will randomly generate the Client id, which is not very easy for jquery to obtain. google has summarized the following three methods:

Server Control Code: <asp: TextBox ID = "txtUserID" runat = "server"> </asp: TextBox>

1. $ ("# <% = txtUserID. ClientID %>"). val ();

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

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

2. Jquery method for obtaining control values

Valid value:

$ ("") Is a jquery object, not a dom element
Value is the attribute of dom element.
Jquery corresponds to val.
Val (): obtains the current value of the First Matching Element.
Val (val): sets the value of each matching element.
Therefore, the code should be written as follows:
Value: val = $ ("# id") [0]. value;
Assignment:
$ ("# Id") [0]. value = "new value ";
Or $ ("# id"). val ("new value ");


Alternatively, you can: val = $ ("# id"). attr ("value ");
Obtains the value of a set of radio selected items.

Var item = $ ('input [@ name = items] [@ checked] '). val ();
Obtain the text of the selected select item.
Var item = $ ("select [@ name = items] option [@ selected]"). text ();
The second element in the select drop-down box is the selected value.
$ ('# Select_id') [0]. selectedIndex = 1;
The second element of the radio Group is the currently selected value.
$ ('Input [@ name = items] '). get (1). checked = true;

Get value:

Text Box, text area: $ ("# txt"). attr ("value ");
Multiple choice box checkbox: $ ("# checkbox_id"). attr ("value ");
Radio Group radio: $ ("input [@ type = radio] [@ checked]"). val ();
Drop-down box select: $ ('# sel'). val ();

Control form elements:
Text Box, text area: $ ("# txt"). attr ("value", ''); // clear the content
$ ("# Txt"). attr ("value", '11'); // fill in the content

Multiple selection box checkbox:$ ("# Chk1"). attr ("checked", ''); // do not check
$ ("# Chk2"). attr ("checked", true); // check
If ($ ("# chk1"). attr ('checked') = undefined) // you can check whether the checked has been checked.

Radio Group radio:$ ("Input [@ type = radio]"). attr ("checked", '2'); // set the project with value = 2 to the selected one
Drop-down box select:$ ("# Sel"). attr ("value", '-sel3'); // set the value =-sel3 project to the currently selected item
$ ("<Option value = '1'> 1111 </option> <option value = '2'> 2222 </option> "). appendTo ("# sel") // Add the option in the drop-down box


$ ("# Sel"). empty (); // clear the drop-down list

3. Jquery method for obtaining the DropDownList value of the control
Copy codeThe Code is as follows:
<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", );
}
</Script>
<Html>
<Asp: DropDownList ID = "ddlGuo" runat = "server">
<Asp: ListItem Selected = "True" Value = "001"> Beijing </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 Select" OnClientClick = "bbOK ();"/>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.