Jquery select operations

Source: Internet
Author: User

Add option
Copy codeThe Code is as follows:
$ ('# Id'). append ("<option value =" value "> Text </option>"); // append an option to select
$ ('# Id'). prepend ("<option value = '0'> Text </option>"); // insert an option for select


Remove option
Copy codeThe Code is as follows:
$ ("# ID option"). each (function (){
If ($ (this). val () = 111 ){
$ (This). remove ();
}
});

$ ("<Option value = '000000'> UPS Ground </option>"). appendTo ($ ("# ID "));
Obtain the selected value from the drop-down list.
$ ("# TestSelect option: selected"). text ();
$ ("# TestSelect"). find ('option: selected'). text ();
$ ("# TestSelect"). val ();
Select the drop-down Box Based on the option value
$ ('# Testselect'). val ('20140901 ');

2, single region:

Copy codeThe Code is as follows:
$ ("Input [@ type = radio] [@ checked]"). val (); // obtain the value of the selected item of a single sequence (note that there is no space in the middle)
$ ("Input [@ type = radio] [@ value = 2]"). attr ("checked", 'checked'); // set single checked value = 2 to the selected status. (Note that there is no space in the middle)
3. Check box:
$ ("Input [@ type = checkbox] [@ checked]"). val (); // obtain the value of the first check box.
$ ("Input [@ type = checkbox] [@ checked]"). each (function () {// Since multiple check boxes are selected, You can output them cyclically.
Alert ($ (this). val ());
});

$ ("# Chk1"). attr ("checked", ''); // do not check
$ ("# Chk2"). attr ("checked", true); // check
If ($ ("# chk1"). attr ('checked') = undefined) {}// you can check whether a check has been performed.
Of course, jquery's selector is powerful. There are many other methods.

Copy codeThe Code is as follows:
<Script src = "jquery-1.2.1.js" type = "text/javascript"> </script>
<Script language = "javascript" type = "text/javascript">
$ (Document). ready (function (){
$ ("# SelectTest"). change (function ()
{
Alert ($ ("# selectTest option [@ selected]"). text ());
$ ("# SelectTest"). attr ("value", "2 ");

});
});
</Script>

<A href = "#"> aaass </a>

<! -- Drop-down box -->
<Select id = "selectTest" name = "selectTest">
<Option value = "1"> 11 </option>
<Option value = "2"> 22 </option>
<Option value = "3"> 33 </option>
<Option value = "4"> 44 </option>
<Option value = "5"> 55 </option>
<Option value = "6"> 66 </option>
</Select>

Jquery radio value, checkbox value, select value, radio selected, checkbox selected, select selected, and related to obtain the value of a set of radio selected items
Copy codeThe Code is as follows:
Varitem = $ ('input [@ name = items] [@ checked] '). val ();

Obtain the text of the selected select item.
Copy codeThe Code is as follows:
Varitem = $ ("select [@ name = items] option [@ selected]"). text ();

The second element in the select drop-down box is the selected value.
Copy codeThe Code is as follows:
$ ('# Select_id') [0]. selectedIndex = 1;

The second element of the radio Group is the currently selected value.
Copy codeThe Code is as follows:
$ ('Input [@ name = items] '). get (1). checked = true;

Get value:
Text Box, text area:
Copy codeThe Code is as follows:
$ ("# Txt"). attr ("value ");

Multiple selection box checkbox:
Copy codeThe Code is as follows:
$ ("# Checkbox_id"). attr ("value ");

Radio Group radio:
Copy codeThe Code is as follows:
$ ("Input [@ type = radio] [@ checked]"). val ();

Drop-down box select:
Copy codeThe Code is as follows:
$ ('# Sel'). val ();

Control form elements:
Text Box, text area:
Copy codeThe Code is as follows:
$ ("# Txt"). attr ("value", ''); // clear the content
$ ("# Txt"). attr ("value", '11'); // fill in the content

Multiple selection box checkbox:
Copy codeThe Code is as follows:
$ ("# 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:
Copy codeThe Code is as follows:
$ ("# Sel"). attr ("value", '-sel3'); // set the value =-sel3 project to the currently selected item
$ ("<Optionvalue = '1'> 1111 </option> <optionvalue = '2'> 2222 </option> "). appendTo ("# sel") // Add the option in the drop-down box
$ ("# Sel"). empty (); // clear the drop-down list

Obtains the value of a set of radio selected items.
Copy codeThe Code is as follows:
Varitem = $ ('input [@ name = items] [@ checked] '). val ();

Obtain the text of the selected select item.
Copy codeThe Code is as follows:
Varitem = $ ("select [@ name = items] option [@ selected]"). text ();

The second element in the select drop-down box is the selected value.
Copy codeThe Code is as follows:
$ ('# Select_id') [0]. selectedIndex = 1;

The second element of the radio Group is the currently selected value.
Copy codeThe Code is as follows:
$ ('Input [@ name = items] '). get (1). checked = true;

Get value:
Text Box, text area:
Copy codeThe Code is as follows:
$ ("# Txt"). attr ("value ");

Multiple selection box checkbox:
Copy codeThe Code is as follows:
$ ("# Checkbox_id"). attr ("value ");

Radio Group radio:
Copy codeThe Code is as follows:
$ ("Input [@ type = radio] [@ checked]"). val ();

Drop-down box select:
Copy codeThe Code is as follows:
$ ('# Sel'). val ();

Control form elements:
Text Box, text area:
Copy codeThe Code is as follows:
$ ("# Txt"). attr ("value", ''); // clear the content
$ ("# Txt"). attr ("value", '11'); // fill in the content

Multiple selection box checkbox:
Copy codeThe Code is as follows:
$ ("# 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:
Copy codeThe Code is as follows:
$ ("Input [@ type = radio]"). attr ("checked", '2'); // set the project with value = 2 to the selected one

Drop-down box select:
Copy codeThe Code is as follows:
$ ("# 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

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.