Home  >  Article  >  Web Front-end  >  Examples of jQuery operating checkbox to implement multiple selections and deselections

Examples of jQuery operating checkbox to implement multiple selections and deselections

黄舟
黄舟Original
2018-05-15 13:49:553626browse

In our previous article, we introduced to you a detailed explanation of the jQuery method of operating checkbox. Now that we have understood the checkbox method, how do we use checkbox to achieve multiple selections? Today we will introduce to you an example of using jQuery to operate checkbox to achieve multiple selections and deselections!

First let’s take a look at the renderings:

<html>
<head>
    <title></title>
    <script src="jquery-1.8.3.min.js"></script>
    <script>
        function check() {
            var code = "";
            //$("input[name=chkname][value=AAAAA],[value=BBBBB]").attr("checked", true);//固有属性
            $("input[name=chkname][value=AAAAA],[value=BBBBB]").prop("checked", true);//自定义的DOM属性
        }
        function clearcheck() {
            $("input[name=chkname]").removeAttr("checked");
        }
    </script>
</head>
<body>
    <input type="checkbox" value="AAAAA" name="chkname" />AAAAA
    <input type="checkbox" value="BBBBB" name="chkname" />BBBBB
    <input type="checkbox" value="CCCCC" name="chkname" />CCCCC
    <input type="checkbox" value="DDDDD" name="chkname" />DDDDD
    <br />
    <input id="btnCheck" type="button" value="选中[value=AAAAA],[value=BBBBB]" onclick="check()" />
    <input id="btnClear" type="button" value="取消选中" onclick="clearcheck()" />
    <br />
  
<pre class="brush:php;toolbar:false">
对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。
对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。
如果在ajax加载方式,attr无效时就使用prop。

<html>
<head>
<title></title>
<script src="jquery-1.8.3.min.js"></script>
<script>
function check() {
var code = "";
$("input[name=chkname][value=AAAAA],[value=BBBBB]").attr("checked", true);
}
function clearcheck() {
$("input[name=chkname]").removeAttr("checked");
}
</script>
</head>
<body>
<input type="checkbox" value="AAAAA" name="chkname" />AAAAA
<input type="checkbox" value="BBBBB" name="chkname" />BBBBB
<input type="checkbox" value="CCCCC" name="chkname" />CCCCC
<input type="checkbox" value="DDDDD" name="chkname" />DDDDD
<br />
<input id="btnCheck" type="button" value="选中[value=AAAAA],[value=BBBBB]" onclick="check()" />
<input id="btnClear" type="button" value="取消选中" onclick="clearcheck()" />
</body>
</html>

Summary:

This article uses the effect of graphic code to show you an example of jQuery operating checkbox to achieve multiple selections and deselections. In this regard, friends have a better understanding of checkboxes to achieve multiple selections. I hope it will be helpful to your work!

Related recommendations:

Detailed examples of jQuery methods to operate CheckBox

jquery one-click method to control checkbox selection, inverse selection, and no selection at all

How to get the selected items in the checkbox in js Multiple values

checkbox implements click event triggers span element content change

The above is the detailed content of Examples of jQuery operating checkbox to implement multiple selections and deselections. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:How to use jquery apiNext article:How to use jquery api