JQuery obtains the number and value of the check box.

Source: Internet
Author: User

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>

 

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.