As Jquery becomes more and more useful, more and more friends are using it. In the Web, because the CheckBox, Radiobutton, DropDownList, and other controls are frequently used, they are related to the operations of these controls in Jquery. The Jquery version is updated quickly, and the code writing has changed a lot. The following Jquery Code is applicable to query1.4 and later versions.
Radio
1. to obtain the selected value, you can: $ ('input: radio: checked '). val (); $ ("input [type = 'Radio ']: checked "). val (); $ ("input [name = 'rd']: checked "). val (); 2. set the first Radio to the selected value: $ ('input: radio: first '). attr ('checked', 'checked'); or $ ('input: radio: first '). attr ('checked', 'true'); Note: attr ("checked", 'checked') = attr ("checked", 'true ') = attr ("checked", true) 3. set the last Radio to the selected value: $ ('input: radio: lay '). attr ('checked', 'checked'); or $ ('input: radio: lae '). attr ('checked', 'true'); 4. set any radio as the selected value based on the index value: $ ('input: radio '). eq (index value ). attr ('checked', 'true'); index value = 0, 1, 2 .... or $ ('input: radio '). slice (1, 2 ). attr ('checked', 'true'); 5. set Radio as the selected Value based on the value $ ("input: radio [Value = http://www.bkjia.com/kf/201110/'rd2']"). attr ('checked', 'true'); or $ ("input [value = http://www.bkjia.com/kf/201110/'rd2']"). attr ('checked', 'true'); 6. delete Radio $ whose Value is rd2 ("input: radio [value = http://www.bkjia.com/kf/201110/'rd2']"). remove (); 7. delete the number of Radio $ ("input: radio "). eq (index value ). remove (); index value = 0, 1, 2 .... for example, delete 3rd Radio: $ ("input: radio "). eq (2 ). remove (); 8. traverse Radio $ ('input: radio '). each (function (index, domEle) {// write code });
DropDownList
1. get selected item: Get the Value of the selected item: $ ('select # sel option: selected '). val (); or $ ('select # sel '). find ('option: selected '). val (); get the Text value of the selected item: $ ('select # seloption: selected '). text (); or $ ('select # sel '). find ('option: selected '). text (); 2. obtain the index value of the selected item: $ ('select # sel '). get (0 ). selectedIndex; 3. obtain the maximum index value of the current option: $ ('select # sel option: la '). attr ("index") 4. get the DropdownList length: $ ('select # sel ') [0]. options. length; or $ ('select # sel '). get (0 ). options. length; 5. set the first option to the selected value: $ ('select # sel option: first '). attr ('selected', 'true') or $ ('select # sel ') [0]. selectedIndex = 0; 6. set the last option to the selected value: $ ('select # sel option: la '). attr ('selected', 'true ')