This article summarizes jQuery's select operations. For more information, see
// Traverse option and add or 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 {
$ (" UPS Ground"). AppendTo ($ (" select [@ name = ISHIPTYPE] ");
}
}
// Obtain the selected value from the drop-down list
$ ('# 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 from 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"); // obtain the ID attribute value of the selected item from the drop-down menu
$ ("# Select"). empty (); // clear the drop-down box // $ ("# select" example .html ('');
$ (" 1111"). AppendTo (" # select ") // Add the option in the drop-down box
A little explanation:
1. select [@ name = 'country'] option [@ selected] indicates that the parameter has the name attribute.,
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 (); // 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.