Home >Web Front-end >Front-end Q&A >How to implement the negation method in jquery
The jquery method to achieve inversion: 1. Import the jQuery library; 2. Set the click event; 3. Loop to set the selected status of other multi-select boxes; 4. Implement the inversion of the identification variable through the "!chkall;" method That’s it.
The operating environment of this article: windows7 system, jquery-2.1.4 version, Dell G3 computer.
How to implement the inversion method in jquery?
Several methods of using JQuery to achieve all selection and inverse selection
As shown in the figure: the effect to be achieved is to click the Select All box to select all, and then click to unselect all
Method 1: 1. Import the jQuery library, which can be downloaded online and is free and open source. 2. Set the class to fruit and set their properties through prop.
Method 1: jQuery code:
//定义标识,true表示选中 var chkall = true; $(function () { //全选按钮设置点击事件 $("#btnAll").click(function () { //1、循环设置其它多选框选中状态,跟标识用的变量一样 $(".fruit").prop("checked", chkall); //2、标识的变量取反 chkall = !chkall; }) })
Method 2 idea: Get all the radio button boxes through the form filter, and then set the checked attribute in a loop.
Method 2: jQuery code:
$(function () { //得到全选框的值 var $checked = this.checked; //通过表单过滤器得到所有单选框 $("input[class=fruit]").each(function () { //单选框的值与全选框的值保持一致 this.checked = $checked; }) }) })
Final additional explanation: In JQuery, we generally use Attr to obtain attributes, and we use prop to set the value of attributes.
Recommended learning: " jquery video tutorial》
The above is the detailed content of How to implement the negation method in jquery. For more information, please follow other related articles on the PHP Chinese website!