日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jQuery全选反选实例

發布時間:2024/4/17 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jQuery全选反选实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. $('#tb:checkbox').each(function(){ 每次都會執行

全選-取消操作,注意$('#tb :checkbox').prop('checked',true); tb后面一定得有括號,否則會報錯。

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><input type="button" value="全選" οnclick="checkAll();"/><input type="button" value="反選" οnclick="reverseAll();"/><input type="button" value="取消" οnclick="cancelAll();"/><table border="1"><thead><tr><th>選項</th><th>IP</th><th>port</th></tr></thead><tbody id="tb"><tr><td><input type="checkbox"></td><td>1.1.1.1</td><td>80</td></tr><tr><td><input type="checkbox"></td><td>1.1.1.1</td><td>80</td></tr><tr><td><input type="checkbox"></td><td>1.1.1.1</td><td>80</td></tr></tbody></table><script src="jquery-1.12.4.js"></script><script>function checkAll(){$('#tb :checkbox').prop('checked',true);}function cancelAll(){$('#tb :checkbox').prop('checked',false);}</script></body> </html>

?用DOM實現 全選-反選-取消操作:

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><input type="button" value="全選" οnclick="checkAll();"/><input type="button" value="反選" οnclick="reverseAll();"/><input type="button" value="取消" οnclick="cancelAll();"/><table border="1"><thead><tr><th>選項</th><th>IP</th><th>port</th></tr></thead><tbody id="tb"><tr><td><input type="checkbox"></td><td>1.1.1.1</td><td>80</td></tr><tr><td><input type="checkbox"></td><td>1.1.1.1</td><td>80</td></tr><tr><td><input type="checkbox"></td><td>1.1.1.1</td><td>80</td></tr></tbody></table><script src="jquery-1.12.4.js"></script><script>function checkAll(){$('#tb :checkbox').prop('checked',true);}function cancelAll(){$('#tb :checkbox').prop('checked',false);}function reverseAll(){$('#tb :checkbox').each(function(){//this 代指當前循環的每一個元素,this是DOM對象。//console.log(this);
        //用DOM實現的if(this.checked){this.checked=false;}else{this.checked=true;}})}</script></body> </html>

?效果:

2.用 jQuery實現 全選-反選-取消操作:

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><input type="button" value="全選" οnclick="checkAll();"/><input type="button" value="反選" οnclick="reverseAll();"/><input type="button" value="取消" οnclick="cancelAll();"/><table border="1"><thead><tr><th>選項</th><th>IP</th><th>port</th></tr></thead><tbody id="tb"><tr><td><input type="checkbox"></td><td>1.1.1.1</td><td>80</td></tr><tr><td><input type="checkbox"></td><td>1.1.1.1</td><td>80</td></tr><tr><td><input type="checkbox"></td><td>1.1.1.1</td><td>80</td></tr></tbody></table><script src="jquery-1.12.4.js"></script><script>function checkAll(){$('#tb :checkbox').prop('checked',true);}function cancelAll(){$('#tb :checkbox').prop('checked',false);}function reverseAll(){$('#tb :checkbox').each(function(){//傳2個參數表示設置值,傳1個參數表示獲取值。if($(this).prop('checked')){$(this).prop('checked',false);}else{$(this).prop('checked',true);}})}</script></body> </html>

?3. 三元運算

var v=條件?真值:假值

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><input type="button" value="全選" οnclick="checkAll();"/><input type="button" value="反選" οnclick="reverseAll();"/><input type="button" value="取消" οnclick="cancelAll();"/><table border="1"><thead><tr><th>選項</th><th>IP</th><th>port</th></tr></thead><tbody id="tb"><tr><td><input type="checkbox"></td><td>1.1.1.1</td><td>80</td></tr><tr><td><input type="checkbox"></td><td>1.1.1.1</td><td>80</td></tr><tr><td><input type="checkbox"></td><td>1.1.1.1</td><td>80</td></tr></tbody></table><script src="jquery-1.12.4.js"></script><script>function checkAll(){$('#tb :checkbox').prop('checked',true);}function cancelAll(){$('#tb :checkbox').prop('checked',false);}function reverseAll(){$('#tb :checkbox').each(function(){var v=$(this).prop('checked')?false:true;$(this).prop('checked',v);})}</script></body> </html>

?4. 筆記

實例:全選,反選,取消-選擇器-$('#tb :checkbox').prop('checked'); 獲取值$('#tb :checkbox').prop('checked',true); 賦值-jQuery方法內置循環: $('#tb :checkbox').xxxx$('#tb :checkbox').each(function(k){//k是當前索引//this,DOM,當前循環的元素$(this)})-三元運算:var v=條件?真值:假值

?

轉載于:https://www.cnblogs.com/momo8238/p/7463587.html

總結

以上是生活随笔為你收集整理的jQuery全选反选实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。