JQuery implements three control methods (drop-down, single-choice, and check)

Source: Internet
Author: User

Radion <input type = "radio" name = "rd" id = "rd1" checked = "checked" value = "rd1"/> <input type = "radio" name =" rd "id =" rd2 "value =" rd2 "/> <input type =" radio "name =" rd "id =" rd3 "value =" rd3 "/> 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 = 'rd2 ']"). attr ('checked', 'true'); or $ ("input [value = 'rd2 ']"). attr ('checked', 'true'); 6. delete Radio $ ("input: radio [Value = 'rd2 ']") whose value is 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 <select id = "sel"> <option value = "1" selected = "selected"> a </option> <option value = "2"> B </option> <option value = "3"> c </option> <option value = "4"> d </option> <option value = "5"> e </option> </select> 1. obtain 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: last ). attr ('selected', 'true') 7. set any option to the selected value based on the index value: $ ('select # sel ') [0]. selectedIndex = index value; index value = 0, 1, 2 .... 8. set option of Value = 4 to the selected Value: $ ('select # sel '). attr ('value', '4'); or $ ("select # sel option [value = '4']"). attr ('selected', 'true'); 9. delete option: $ ("select # sel option [Value = '3']") for value = 3. remove (); 10. delete the number of options: $ ("select # sel option "). eq (index value ). remove (); index value = 0, 1, 2 .... for example, delete 3rd Radio: $ ("select # sel option "). eq (2 ). remove (); 11. delete the first option: $ ("select # sel option "). eq (0 ). remove (); or $ ("select # sel option: first "). remove (); 12. delete the last option: $ ("select # sel option: last "). remove (); 13. delete dropdownlist: $ ("select # sel "). remove (); 14. add an option: $ ("select # sel") After select "). append ("<option value = '6'> f </option>"); 15. add an option: $ ("select # sel") before select "). prepend ("<option value = '0'> 0 </option>"); 16. traverse option: $ ('select # sel option '). each (function (index, domEle) {// write code }); checkBox <input type = "checkbox" id = "ck1" name = "ck" vlaue = "1" checked = "checked"/> <input type = "checkbox" id =" ck2 "name =" ck "vlaue =" 2 "/> <input type =" checkbox "id =" ck3 "name =" ck "vlaue =" 3 "/> <input type = "checkbox" id = "ck4" name = "ck" vlaue = "4"/> 1. get a single checkbox selected item (three methods): $ ("input: checkbox: checked "). val () or $ ("input: [type = 'checkbox']: checked "). val (); or $ ("input: [name = 'ck ']: checked "). val (); 2. obtain multiple checkbox selected items: $ ('input: checkbox '). each (function () {if ($ (this ). attr ('checked') = true) {alert ($ (this ). val () ;}}); 3. set the first checkbox to the selected value: $ ('input: checkbox: first '). attr ("checked", 'checked'); or $ ('input: checkbox '). eq (0 ). attr ("checked", 'true'); 4. set the last checkbox to the selected value: $ ('input: radio: lay '). attr ('checked', 'checked'); or $ ('input: radio: lae '). attr ('checked', 'true'); 5. set any checkbox as the selected value based on the index value: $ ('input: checkbox ). eq (index value ). attr ('checked', 'true'); index value = 0, 1, 2 .... or $ ('input: radio '). slice (1, 2 ). attr ('checked', 'true'); 6. multiple checkboxes are selected: 1st and 2nd checkboxes are selected at the same time: $ ('input: radio '). slice (0, 2 ). attr ('checked', 'true'); 7. set checkbox as the selected Value based on the value: $ ("input: checkbox [Value = '1']"). attr ('checked', 'true'); 8. delete the checkbox: $ ("input: checkbox [Value = '1']") with value = 1. remove (); 9. delete the number of checkpoints: $ ("input: checkbox "). eq (index value ). remove (); index value = 0, 1, 2 .... for example, delete 3rd checkboxes: $ ("input: checkbox "). eq (2 ). remove (); 10. traverse checkbox: $ ('input: checkbox '). each (function (index, domEle) {// write code}); 11. select all $ ('input: checkbox '). each (function () {$ (this ). attr ('checked', true) ;}); 12. unselect all: $ ('input: checkbox '). each (function () {$ (this ). attr ('checked', false );});

 

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.