Common js/jQuery operations: Frequently asked questions about jQuery operation check boxes
Mobile
2023-12-16 23:24:53
views: null
Common js/jQuery operations: Frequently asked questions about jQuery operation check boxes
1. Some other common basic operations of js/jQuery
2. Select all/unselect all questions
2.1 Effect
as follows:
2.2 Implementation code
2.2.1 Simple js implementation
as follows: <! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> </ title>
</ head>
< body>
< form method = " post" action = " " >
您想饲养的狗狗有?
< input type = " checkbox" id = " checkAllOrNo" /> 全选/全不选
< br />
< input type = " checkbox" name = " items" value = " 边牧" /> 边牧
< input type = " checkbox" name = " items" value = " 柯基" /> 柯基
< input type = " checkbox" name = " items" value = " 秋田犬" /> 秋田犬
< input type = " checkbox" name = " items" value = " 金毛" /> 金毛
< br />
< input type = " button" id = " checkAll" value = " 全选" />
< input type = " button" id = " checkNoOne" value = " 全不选" />
< input type = " button" id = " fanxuan" value = " 反选" />
< input type = " button" id = " btnAllOrNO" value = " 全选/全不选" />
< input type = " button" id = " commit" value = " 提交" />
</ form>
< script type = " text/javascript" >
var all = document. getElementById ( "checkAll" ) ;
function funAll ( ) {
var box = document. getElementsByName ( "items" ) ;
for ( var i = 0 ; i< box. length; i++ ) {
box[ i] . checked = true ;
}
}
all. onclick = funAll;
var allNo = document. getElementById ( "checkNoOne" ) ;
function funAllNo ( ) {
var box = document. getElementsByName ( "items" ) ;
for ( var i = 0 ; i< box. length; i++ ) {
box[ i] . checked = false ;
}
}
allNo. onclick = funAllNo;
var fan = document. getElementById ( "fanxuan" ) ;
fan. onclick = function ( ) {
var box = document. getElementsByName ( "items" ) ;
for ( var i = 0 ; i< box. length; i++ ) {
box[ i] . checked = ! box[ i] . checked;
}
}
var btn_check_all = document. getElementById ( "btnAllOrNO" ) ;
btn_check_all. onclick = function ( ) {
var boxes = document. getElementsByName ( "items" ) ;
var flag = false ;
for ( var i = 0 ; i< boxes. length; i++ ) {
if ( boxes[ i] . checked == false ) {
flag = true ;
break ;
}
}
if ( flag) {
funAll ( ) ;
} else {
funAllNo ( ) ;
}
}
var checkAllOrNo_2 = document. getElementById ( "checkAllOrNo" ) ;
checkAllOrNo_2. onclick = function ( ) {
var box = document. getElementsByName ( "items" ) ;
for ( var i = 0 ; i< box. length; i++ ) {
box[ i] . checked = this . checked;
}
}
var boxAll_2 = document. getElementsByName ( "items" ) ;
for ( var i= 0 ; i< boxAll_2. length; i++ ) {
boxAll_2[ i] . onclick = function ( ) {
checkAllOrNo_2. checked = true ;
for ( var i= 0 ; i< boxAll_2. length; i++ ) {
if ( boxAll_2[ i] . checked == false ) {
checkAllOrNo_2. checked = false ;
break ;
}
}
}
}
var data = document. getElementById ( "commit" ) ;
data. onclick = function ( ) {
var box = document. getElementsByName ( "items" ) ;
var commmitData = "" ;
for ( var i = 0 ; i< box. length; i++ ) {
if ( box[ i] . checked == true ) {
commmitData += box[ i] . value + "," ;
}
}
var resultData = commmitData. substring ( 0 , commmitData. length- 1 ) ;
alert ( resultData) ;
}
</ script>
</ body>
</ html>
2.2.2 jQuery implementation
2.2.2.1 Pay attention to the syntax (difference between jQuery versions)
The following is select all For example, as follows:
Complex writing:
var all = document. getElementById ( "checkAll" ) ;
function funAll ( ) {
$ ( "input[name='items']" ) . each ( function ( ) {
this . checked = true ;
} ) ;
}
all. onclick = funAll;
Optimized writing: $ ( "#checkAll" ) . click ( function ( ) {
$ ( "input[name='items']" ) . attr ( "checked" , true ) ;
} ) ;
again Select all/Deselect all For example (non-button case), as follows:
Click to select all/unselect all - more complex logic:
$ ( "#checkAllOrNo" ) . click ( function ( ) {
var parentFlag = this . checked;
$ ( "input[name='items']" ) . each ( function ( ) {
this . checked = parentFlag;
} ) ;
} ) ;
Click Select All/Deselect All—— Simple logic :
$ ( "#checkAllOrNo" ) . click ( function ( ) {
$ ( "input[name='items']" ) . each ( function ( ) {
this . checked = $ ( "#checkAllOrNo" ) . is ( ":checked" ) ;
} ) ;
} ) ;
Click on each item:
$ ( "input[name='items']" ) . each ( function ( ) {
$ ( this ) . click ( function ( ) {
$ ( "#checkAllOrNo" ) . attr ( "checked" , true ) ;
$ ( "input[name='items']" ) . each ( function ( ) {
if ( ! this . checked) {
$ ( "#checkAllOrNo" ) . attr ( "checked" , false ) ;
return ;
}
} ) ;
} ) ;
} ) ;
For more information about the jQuery version, you can read the following article, which is introduced in 3.6. Some common operations of js/jQuery (js/jQuery obtains form element values and clears element values) Various implementation methods) - attached with test examples, you can achieve the effect immediately .
2.2.2.2 Complete code implementation
The codes for each situation are as follows: < ! DOCTYPE html>
< html>
< head>
< meta charset= "utf-8" >
< title> < / title>
< ! -- < script src= "https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.js" > < / script> -- >
< script src= "https://cdn.bootcdn.net/ajax/libs/jquery/1.3.2/jquery.min.js" > < / script>
< / head>
< body>
< form method= "post" action= "" >
您想饲养的狗狗有?
< input type= "checkbox" id= "checkAllOrNo" / > 全选/ 全不选
< br / >
< input type= "checkbox" name= "items" value= "边牧" / > 边牧
< input type= "checkbox" name= "items" value= "柯基" / > 柯基
< input type= "checkbox" name= "items" value= "秋田犬" / > 秋田犬
< input type= "checkbox" name= "items" value= "金毛" / > 金毛
< br / >
< input type= "button" id= "checkAll" value= "全选" / >
< input type= "button" id= "checkNoOne" value= "全不选" / >
< input type= "button" id= "fanxuan" value= "反选" / >
< input type= "button" id= "btnAllOrNO" value= "全选/全不选" / >
< input type= "button" id= "commit" value= "提交" / >
< / form>
< script type= "text/javascript" >
var all = document. getElementById ( "checkAll" ) ;
function funAll ( ) {
$ ( "input[name='items']" ) . each ( function ( ) {
this . checked = true ;
} ) ;
}
$ ( "#checkAll" ) . click ( function ( ) {
$ ( "input[name='items']" ) . attr ( "checked" , true ) ;
} ) ;
$ ( "#checkNoOne" ) . click ( function ( ) {
$ ( "input[name='items']" ) . attr ( "checked" , false ) ;
} ) ;
$ ( "#fanxuan" ) . click ( function ( ) {
$ ( "input[name='items']" ) . each ( function ( ) {
this . checked = ! ( this . checked) ;
} ) ;
} ) ;
$ ( "#btnAllOrNO" ) . click ( function ( ) {
var selectedNum = 0 ;
$ ( "input[name='items']:not(:checked)" ) . each ( function ( ) {
selectedNum ++ ;
} ) ;
if ( selectedNum > 0 ) {
$ ( "input[name='items']" ) . attr ( "checked" , true ) ;
} else {
$ ( "input[name='items']" ) . attr ( "checked" , false ) ;
}
} ) ;
$ ( "#checkAllOrNo" ) . click ( function ( ) {
$ ( "input[name='items']" ) . each ( function ( ) {
this . checked = $ ( "#checkAllOrNo" ) . is ( ":checked" ) ;
} ) ;
} ) ;
$ ( "input[name='items']" ) . each ( function ( ) {
$ ( this ) . click ( function ( ) {
$ ( "#checkAllOrNo" ) . attr ( "checked" , true ) ;
$ ( "input[name='items']" ) . each ( function ( ) {
if ( ! this . checked) {
$ ( "#checkAllOrNo" ) . attr ( "checked" , false ) ;
return ;
}
} ) ;
} ) ;
} ) ;
$ ( "#commit" ) . click ( function ( ) {
var commmitData = "" ;
$ ( "input[name='items']:checked" ) . each ( function ( ) {
commmitData += $ ( this ) . val ( ) + "," ;
} ) ;
var resultData = commmitData. substring ( 0 , commmitData. length- 1 ) ;
alert ( resultData) ;
} ) ;
$ ( "#commit" ) . click ( function ( ) {
var commmitData = [ ] ;
$ ( "input[name='items']:checked" ) . each ( function ( ) {
commmitData. push ( $ ( this ) . val ( ) ) ;
} ) ;
alert ( commmitData) ;
} ) ;
< / script>
< / body>
< / html>
3. jQuery realizes click on row tr to realize checkBox selection + page turning check problem
3.1 jQuery implements clicking row tr to obtain the value of checkBox in td
3.1.1 Method 1
3.1.2 Method 2
3.2 jQuery implements clicking on row tr to implement checkBox selection or cancellation
3.2.1 When tr is clicked, the checkbox is selected or canceled
3.2.2 When clicking on the first line tr, select all or none
3.2.3 When clicking on the non-first row tr (data list row), it will automatically switch between selecting all or not selecting all.
The implementation code is as follows:
$ ( "#dogs_data tr:gt(0)" ) . each ( function ( ) {
$ ( this ) . click ( function ( ) {
var box = this . firstElementChild. firstElementChild;
box. checked = ! box. checked;
$ ( "#checkAllOrNo" ) . attr ( "checked" , true ) ;
$ ( "input[name='dogIds']:not(:checked)" ) . each ( function ( ) {
$ ( "#checkAllOrNo" ) . attr ( "checked" , false ) ;
return ;
} ) ;
} ) ;
} ) ;
The effect is as follows (not a video, it is not very obvious, you need to test it yourself if you need it):
3.3 Page turning and checking questions
If you want to support page turning check, then cache each check in an input box on the page, and then add one after each check. This can be achieved by directly splicing with reference to the above value method. Here is No more words.
3.4 Attached is the full code of the above operation tr
as follows: < ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< meta name= "viewport" content= "width=device-width, initial-scale=1.0" >
< title> Document< / title>
< ! -- < script src= "https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.js" > < / script> -- >
< script src= "https://cdn.bootcdn.net/ajax/libs/jquery/1.3.2/jquery.min.js" > < / script>
< style>
table {
border- collapse: collapse;
}
table tr th, td{
border : 1px solid;
width : 60px;
text- align: center;
}
thead tr: hover{
background- color: rgb ( 255 , 127 , 127 ) ;
}
tbody tr: hover{
background- color: aquamarine;
}
< / style>
< / head>
< body>
< table celllspacing= "0" id= "dogs_data" >
< thead>
< tr>
< th>
< input type= "checkbox" id= "checkAllOrNo" / >
< / th>
< th> 编号< / th>
< th> 姓名< / th>
< th> 年龄< / th>
< / tr>
< / thead>
< tbody>
< tr>
< td>
< input type= "checkbox" name= "dogIds" value= "A1001" / >
< / td>
< td> A1001 < / td>
< td> 麦兜< / td>
< td> 3 < / td>
< / tr>
< tr>
< td>
< input type= "checkbox" name= "dogIds" value= "A1002" / >
< / td>
< td> A1002 < / td>
< td> 贝塔< / td>
< td> 2 < / td>
< / tr>
< tr>
< td>
< input type= "checkbox" name= "dogIds" value= "A1003" / >
< / td>
< td> A1003 < / td>
< td> 泡泡< / td>
< td> 6 < / td>
< / tr>
< / tbody>
< / table>
< script>
$ ( "#dogs_data tr:eq(0)" ) . each ( function ( ) {
$ ( this ) . click ( function ( ) {
var box = this . firstElementChild. firstElementChild;
box. checked = ! box. checked;
$ ( "input[name='dogIds']" ) . each ( function ( ) {
this . checked = box. checked;
} ) ;
} ) ;
} ) ;
$ ( "#dogs_data tr:gt(0)" ) . each ( function ( ) {
$ ( this ) . click ( function ( ) {
var box = this . firstElementChild. firstElementChild;
box. checked = ! box. checked;
$ ( "#checkAllOrNo" ) . attr ( "checked" , true ) ;
$ ( "input[name='dogIds']:not(:checked)" ) . each ( function ( ) {
$ ( "#checkAllOrNo" ) . attr ( "checked" , false ) ;
return ;
} ) ;
} ) ;
} ) ;
< / script>
< / body>
< / html>
Origin blog.csdn.net/suixinfeixiangfei/article/details/134722886