JQuery control drop-down list, jquery drop-down list

Source: Internet
Author: User

JQuery control drop-down list, jquery drop-down list

// Traverse option and add and remove option function changeShipMethod (shipping) {var len = $ ("select [@ name = ISHIPTYPE] option"). length if (shipping. value! = "CA") {$ ("select [@ name = ISHIPTYPE] option "). each (function () {if ($ (this ). val () = 111) {$ (this ). remove () ;}}) ;}else {$ ("<option value = '000000'> UPS Ground </option> "). appendTo ($ ("select [@ name = ISHIPTYPE]") ;}// obtain the value of the drop-down menu $ (# testSelect option: selected '). text (); or $ ("# testSelect "). find ('option: selected '). text (); or $ ("# testSelect "). val ();

//////////////////////////////////////// //////////////////////////
If your memory is poor, you can add it to your favorites:
1. drop-down box:

Var C0 = $ (". formc select [@ name = 'country'] option [@ selected] "). text (); // get the text of the selected item in the drop-down menu (note that there is a space in the middle) var cc2 = $ ('. formc select [@ name = "country"] '). val (); // obtain the value of the selected item from the drop-down menu var cc3 = $ ('. formc select [@ name = "country"] '). attr ("id"); // get the ID attribute value of the selected item from the drop-down menu $ ("# select "). empty (); // clear the drop-down box // $ ("# select" example .html (''); $ ("<option value = '1'> 1111 </option> "). appendTo ("# select") // Add the option in the drop-down box

A little explanation:
1. select [@ name = 'country'] option [@ selected] indicates that the name attribute exists,
In addition, the select element with the selected attribute in the 'country' select element has the selected attribute;
It can be seen that the attribute is followed by the @ parameter.

2, single region:

$ ("Input [@ type = radio] [@ checked]"). val (); // get 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 alert ($ (this) cyclically ). val () ;}); $ ("# chk1 "). attr ("checked", ''); // do not tick $ (" # chk2 "). attr ("checked", true); // tick if ($ ("# chk1 "). attr ('checked') = undefined) {} // checks whether the check has been completed.

Of course, jquery's selector is powerful. There are many other methods.

<Script src = "jquery-1.2.1.js" type = "text/javascript"> </script> <script language = "javascript" type = "text/javascript"> $ (document ). ready (function () {$ ("# selectTest "). change (function () {// alert ("Hello"); // alert ($ ("# selectTest "). attr ("name"); // $ (""). attr ("href", "xx.html"); // window. location. href = "xx.html"; // alert ($ ("# selectTest "). val (); alert ($ ("# selectTest option [@ selected]"). text (); $ ("# selec TTest "). 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, select, radio, checkbox, and select, obtain the value of a set of radio selected items var item =$ ('input [@ name = items] [@ checked] '). val (); get the text var item of the selected select item =$ ("select [@ name = items] option [@ selected]"). text (); the second element in the select drop-down box is the currently 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; 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 element: text box, text area: $ ("# txt "). attr ("value", ''); // clear the content $ (" # txt "). attr ("value", '11'); // multiple-choice box for filling content checkbox: $ ("# chk1 "). attr ("checked", ''); // do not tick $ (" # chk2 "). attr ("checked", true); // tick if ($ ("# chk1 "). attr ('checked') = undefined) // determines whether the radio group radio has been checked: $ ("input [@ type = radio]"). attr ("checked", '2'); // select: $ ("# sel") for the project whose value is set to 2 "). attr ("value", '-sel3 '); // set the value =-sel3 project to the selected item $ ("<optionvalue = '1'> 1111 </option> <optionvalue = '2'> 2222 </option> "). appendTo ("# sel") // Add option $ ("# sel") in the drop-down box "). empty (); // clear the drop-down box to obtain the value of a set of radio selected items var item =$ ('input [@ name = items] [@ checked] '). val (); get the text var item of the selected select item =$ ("select [@ name = items] option [@ selected]"). text (); the second element in the select drop-down box is the currently 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; 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 element: text box, text area: $ ("# txt "). attr ("value", ''); // clear the content $ (" # txt "). attr ("value", '11'); // multiple-choice box for filling content checkbox: $ ("# chk1 "). attr ("checked", ''); // do not tick $ (" # chk2 "). attr ("checked", true); // tick if ($ ("# chk1 "). attr ('checked') = undefined) // determines whether the radio group radio has been checked: $ ("input [@ type = radio]"). attr ("checked", '2'); // select: $ ("# sel") for the project whose value is set to 2 "). 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 option $ ("# sel") in the drop-down box "). 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.