The check box is a special html plug-in. Next I will introduce some instance methods for getting the selected number and value of the check box in jquery. If you need to know it, do not reference it.
Obtain the selected value of the check box
| The Code is as follows: |
Copy code |
<Input type = "button" id = "btn5" value = "get all selected values"> <Input type = "text" name = "dd" id = "dd" size = "50"/> $ ("# Btn5"). click (function (){ Var str = ""; $ ("[Name = 'checkbox'] [checked]"). each (function (){ Str + = $ (this). val () + ","; }) $ ("# Dd"). val (str) }) |
JQuery obtains the number of checkboxes selected.
To get the number of items selected in the checkbox through jQuery, you need to use the jQuery size () method or the length attribute. The following example shows how to get the number of items selected in the checkbox through the length attribute.
| The Code is as follows: |
Copy code |
<Ol>
<Li> <input type = "checkbox" name = "test"/> watch TV </li>
<Li> <input type = "checkbox" name = "test"/> watch a movie </li>
<Li> <input type = "checkbox" name = "test"/> surfing the Internet </li>
<Li> <input type = "checkbox" name = "test"/> mountain creation </li>
<Li> <input type = "checkbox" name = "test"/> playground </li>
<Li> <input type = "checkbox" name = "test"/> shopping </li>
<Li> <input type = "checkbox" name = "test"/> Party </li>
</Ol> <P> <input type = "button" id = "count" value = "How many checkboxes are selected? "/> <Script type = "text/javascript"> $ (Document). ready (function (){
$ ('Input [type = checkbox] '). click (function (){ $ (This). attr ('Disabled ', 'Disabled '); If ($ ("input [name = 'test']: checked"). length> = 3) { $ ("Input [name = 'test']"). attr ('Disabled ', 'Disabled '); } });
$ ("# Count"). click (function (){ $ ('Input'). live ('click', function (){ Alert ($ ('input: checked'). length ); }); }) }) </Script> }) }) </Script> |
I separated the code above. Next we will write an instance
| The Code is as follows: |
Copy code |
<Form action = "" name = "myform" id = "myform" method = "post"> <Input type = "checkbox" name = "id" value = "1"/> <br/> <Input type = "checkbox" name = "id" value = "2"/> <br/> <Input type = "checkbox" name = "id" value = "3"/> <br/> <Input type = "button" value = "Submit" id = "btn"/> </Form> <Script type = "text/javascript"> $ ("# Btn"). click (function (){
Var id = $ ("input [name = 'id']: checked "); Var idvalue = id. val (); // Value Var idl = id. length; // number Alert (idvalue ); Alert (idl ); }) </Script> |