Some related actions of jquery on the checkbox

Source: Internet
Author: User

One, select the checkbox by selector:

1. Set an id attribute for the checkbox, selected by the ID selector:

input type = "checkbox" < name = "myBox" id = "chkOne" value = "1" checked="checked"/>

Jquery:

        $("#chkOne").click(function(){});

2. Set a class property for the checkbox, selected by the class selector:

< input type = "checkbox" name = "myBox" class = "chkTwo" value = "1" checked= "Checked"/>

Jquery:

        $(".chkTwo").click(function(){});   3.通过标签选择器和属性选择器来选取:    <input type="checkbox" name="someBox"  value="1" checked="checked" />

input type = "checkbox" < name = "someBox" value = "2"/>

   JQuery:         $("input[name=‘someBox‘]").click(function(){}); 二、对CheckBox的操作:    以这段checkBox代码为例:    <input type="checkbox" name="box"  value="0" checked="checked" />

input type = "checkbox" < name = "box" value = "1"/>

   <input type="checkbox" name="box" value="2" />

   <input type="checkbox" name="box" value="3" />

   1.遍历checkbox用each()方法:        $("input[name=‘box‘]").each(function(){});
   2.设置checkbox被选中用attr();方法:      $("input[name=‘box‘]").attr("checked","checked");  

In HTML, if a check box is selected, the corresponding tag is checked= "checked". But if you use jquery alert ($ ("#id"). attr ("checked") you will be prompted to be "true" instead of "checked", so determine if ("Checked" ==$ ("#id"). attr ("checked")) Is wrong, should be if (true = = $ ("#id"). attr ("checked"))

3. Get the value of the checkbox selected: $ ("input[name= ' box '][checked]"). each (function () {
if (true = = $ (this). attr ("checked")) {
Alert ($ (this). attr (' value '));
} OR: $ ("input[name= ' box ']:checked"). each (function () {
if (true = = $ (this). attr ("checked")) {
Alert ($ (this). attr (' value '));
}
The difference between $ ("input[name= ' box ']:checked") and $ ("Input[name= ' box ')" has not been tried, I tried to use $ ("input[name= ' box ')") to succeed.

4. Get the value of the unchecked checkbox:

$ ("Input[name= ' box ']"). each (function () {
if ($ (this). attr (' checked ') ==false) {
Alert (This). Val ());
}
});

5. Set the values of the checkbox's Value property:

$ (this). attr ("value", value);

Three, generally is to create a JS array to store the value of the Traversal checkbox, create a JS array method:

1. Var array= new Array ();

2. Add data to the array:

Array.push ($ (this). Val ());

3. The array is separated by "," Output:

Alert (Array.join (', '));

  <input type = ' checkbox ' name = "Mybox" class = "Chktwo" value = "2" />

  <input type = ' checkbox ' name = "Mybox" id = "Chkone" value = "2" />

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.