Home > Web Front-end > JS Tutorial > Summary of jquery operations on checkboxes

Summary of jquery operations on checkboxes

PHPz
Release: 2018-10-13 17:00:34
Original
1476 people have browsed it

This article summarizes and introduces some jQuery methods and techniques for operating checkboxes. It is very simple and practical. Friends in need can refer to it.

12 tips for jquery operation checkbox.

1. Get a single checkbox selected item (three writing methods)

$("input:checkbox:checked").val()
Copy after login

or

$("input:[type='checkbox']:checked").val();
Copy after login

or

$("input:[name='ck']:checked").val();
Copy after login

2. Get multiple checkbox selected items

$('input:checkbox').each(function() {
if ($(this).attr('checked') ==true) {
alert($(this).val());
}
});
Copy after login

3. Set the first checkbox to the selected value

$('input:checkbox:first').attr("checked",'checked');
Copy after login

or

$('input:checkbox').eq(0).attr("checked",'true');
Copy after login

4. Set the last checkbox to the selected value

$('input:radio:last').attr('checked', 'checked')
Copy after login

or

$('input:radio:last').attr('checked', 'true')
Copy after login

5. Set according to index value Any checkbox is the selected value

$('input:checkbox).eq(索引值).attr('checked', 'true');
Copy after login

index value = 0,1,2....
or

$('input:radio').slice(1,2).attr('checked', 'true');
Copy after login

6. Select multiple checkboxes and select the first and second checkboxes at the same time

$('input:radio').slice(0,2).attr('checked','true');
Copy after login

7. Set the checkbox to the selected value according to the Value value

$("input:checkbox[value='1']").attr('checked','true');
Copy after login

8. Delete the checkbox with Value=1

$("input:checkbox[value='1']").remove();
Copy after login

9. Delete which checkbox

$("input:checkbox").eq(索引值).remove();
Copy after login

Index value=0,1,2....
If you delete the third checkbox:

$("input:checkbox").eq(2).remove();
Copy after login

10. Traverse the checkbox

$('input:checkbox').each(function (index, domEle) {
//写入代码
});
Copy after login

11. Select all

$('input:checkbox').each(function() {
$(this).attr('checked', true);
});
Copy after login

12. Deselect all

$('input:checkbox').each(function () {
$(this).attr('checked',false);
})
Copy after login

Some related operations of JQuery on CheckBox

1. Select the CheckBox through the selector:

1. Set an id attribute to the CheckBox, through id selector selection:

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

JQuery:

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

2. Set a class for CheckBox Attributes, selected through the class selector:

  <input type="checkbox" name="myBox" class="chkTwo" value="1" checked="checked" />
Copy after login

JQuery:

    $(".chkTwo").click(function(){});
Copy after login

3. Through tags Selector and attribute selector to select:

  <input type="checkbox" name="someBox" value="1" checked="checked" />
  <input type="checkbox" name="someBox" value="2" />
Copy after login

JQuery:

    $("input[name=&#39;someBox&#39;]").click(function(){});
Copy after login

2. Operation of CheckBox:
Take this checkBox code as an example:

  <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" />
Copy after login

1. Use each() to traverse the checkbox Method:

    $("input[name=&#39;box&#39;]").each(function(){});
Copy after login

2. Set the checkbox to be selected using attr(); Method:

   $("input[name=&#39;box&#39;]").attr("checked","checked");
Copy after login

In HTML, if a checkbox is checked, the corresponding tag is checked="checked". But if you use jquery alert($("#id").attr("checked")), you will be prompted that it is "true" instead of "checked", so judge if("checked"==$("#id" ).attr("checked")) is wrong, it should be if(true == $("#id").attr("checked"))

3. Get the value of the selected checkbox :

  $("input[name=&#39;box&#39;][checked]").each(function(){
  if (true == $(this).attr("checked")) {
     alert( $(this).attr(&#39;value&#39;) );
  }
Copy after login

or:

  $("input[name=&#39;box&#39;]:checked").each(function(){
  if (true == $(this).attr("checked")) {
     alert( $(this).attr(&#39;value&#39;) );
  }
Copy after login

$("input[ name='box']: What is the difference between checked") and $("input[name='box']")? I haven't tried it. I tried using $("input[name='box']") and it was successful. .
4. Get the value of the unchecked checkbox:

  $("input[name=&#39;box&#39;]").each(function(){
     if ($(this).attr(&#39;checked&#39;) ==false) {
        alert($(this).val());
      }
   });
Copy after login

5. Set the value of the value attribute of the checkbox:

     $(this).attr("value",值);
Copy after login

[Related tutorial recommendations]

1. JavaScript video tutorial
2. JavaScript online manual
3. bootstrap tutorial

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template