Common Operations of Jquery: checkbox, select value, radio, checkbox, select selection and related, jquerycheckbox
Common Jquery operations: checkbox, select, radio, checkbox, and select:
1. Shadow page elements
Jquery is really easy to use. For example, to control the display and hiding of div, you can do it in one sentence. See the following instructions. $ ("# Id "). show () indicates display: block, $ ("# id "). hide () indicates display: none; $ ("# id "). toggle () switches the visible state of the element. If the element is visible, switch to hidden. If the element is hidden, switch to visible. $ ("# Id" ).css ('display', 'None'); $ ("# id" ).css ('display', 'block '); or $ ("# id") [0]. style. display = 'none'; $ ("# id") returns JQuery. It is a set and certainly has no display attribute.
2. Determine whether the page elements are shadow
<Script> $ (document ). ready (function () {var temp = $ ("# test "). is (": hidden"); // whether to hide vartemp1 =1 ("# test "). is (": visible"); // whether alert (temp); alert (temp1) ;}); </script>
3. Search for a specified Element
$ ("# Id "). find ("# child"); // according to ID $ ("# id "). find (". child "); // according to class $ (" # id "). find ("input [type = 'image']"); // Based on the type
$ ("Div ul li: first"). get () // according to the tag
4. Use each
<script type="text/javascript"> $(document).ready(function() { // $("ul").find("li").each(function(i) { $("#orderedlist").find("li").each(function(i) { $(this).html( $(this).html(); }); }); </script>
5. jquery radio, checkbox, select, radio, checkbox, select, and related
$ ("[Name = 'checkbox']"). attr ("checked", 'true'); // select $ ("[name = 'checkbox']"). removeAttr ("checked"); // deselect the value of the single-choice radio button for jquery. $ ("input [name = 'items ']: checked "). val (); and: judge whether radio is selected and obtain the selected value as follows: function checkradio () {var item = $ (": radio: checked "); var len = item. length; if (len> 0) {alert ("yes -- the selected value is:" + $ (": radio: checked "). val () ;}} gets the value of a set of radio selected items var item =$ ('input [name = items] [checked] '). val (); obtain the select object The text var item =$ ("select [name = items] option [selected]") of the selected item. 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 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 box and get started with jquery. Many things are unfamiliar when you use $ ("# id") to obtain the page input element, found $ ("# id "). the value cannot be obtained. Later, with the help of the great Baidu, the cause of the problem was found: $ ("") is a jquery object, instead of a dom element value, jquery corresponds to the dom element attribute val (): obtains the current value of the First Matching element. Val (val): sets the value of each matching element. Therefore, the code should be written as follows: Value: val = $ ("# id") [0]. value; value: $ ("# id") [0]. value = "new value"; or $ ("# id "). val ("new value"); or you can: val = $ ("# id "). attr ("value"); In jQuery, each is very useful. It is often used to replace javascript's for loop. for example, if there is an each in a function and some condition in each is true, returns the true or false function methodone (){.... $. each (array, function () {if (condition true) {return true ;}});....} it turns out that it is always incorrect. Later, I found that break and continue cannot be used in the each code block. To implement the break and continue functions, I need to use other methods break ---- return false; continue -- use return ture; so when I want to use return true in each to return to this function, it is actually to let the each continue to execute without interrupting the each, therefore, the function cannot return. In addition, the function checkradio () {var item = $ (": radio: checked") is used to determine whether radio is selected and obtain the selected value "); var len = item. length; if (len> 0) {alert ("yes -- the selected value is:" + $ (": radio: checked "). val ());}}