溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

JS如何實現(xiàn)對name是數(shù)組的復選框的全選和反選以及取消選擇

發(fā)布時間:2020-07-19 07:47:29 來源:網(wǎng)絡 閱讀:870 作者:雷頓學院 欄目:開發(fā)技術

JS如何實現(xiàn)對name是數(shù)組的復選框的全選和反選以及取消選擇? form內(nèi)容如下:

因為PHP接收要用 數(shù)組形式的 復選框,正常情況下 JQ可如果是這種

直接使用 $("input[name=ptpt])即可。但是這種php接收的只是最后一個值,字符串。
<label><input type='checkbox' name='ptpt' value='a1' />a1</label>
<label><input type='checkbox' name='ptpt' value='a3' />a3</label>
<label><input type='checkbox' name='ptpt' value='a6' />a6</label>
<label><input type='checkbox' name='ptpt' value='a9' />a9</label>


這樣PHP接收的是一個 ptpt的數(shù)組

<form method="post" id="form1" name="form1" action="" >
<label><input type='checkbox' name='ptpt[1]' value='a1' />a1</label>
<label><input type='checkbox' name='ptpt[3]' value='a3' />a3</label>
<label><input type='checkbox' name='ptpt[6]' value='a6' />a6</label>
<label><input type='checkbox' name='ptpt[9]' value='a9' />a9</label>
<input type="button" value="全選" onclick=""> 
<input type="button" value="反選" onclick=""> 
<input type="button" value="取消全選" onclick=""> 




<script src="jquery-1.7.2.min.js"></script>
<script>
$(function(){
var chks = $(':checkbox[name^="ptpt["]');  //匹配name開頭為 ptpt[ 的部分
$(':button:eq(0)').click(function(){
chks.attr('checked','checked');
})
$(':button:eq(1)').click(function(){
chks.each(function(){
if($(this).attr('checked')=='checked')
$(this).removeAttr('checked');
else
$(this).attr('checked','checked');
});
})
$(':button:eq(2)').click(function(){
chks.removeAttr('checked');
})
})
</script>


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI