美文网首页
jQuery之checkbox全选、反选、取消

jQuery之checkbox全选、反选、取消

作者: 奔跑的老少年 | 来源:发表于2018-08-28 11:20 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<input type="button" value="全选" onclick="checkAll();">
<input type="button" value="反选" onclick="reverseAll();">
<input type="button" value="取消" onclick="cancelAll();">

<table border="1">
    <thead></thead>
    <tr>
        <th>选项</th>
        <th>ip</th>
        <th>port</th>
    </tr>
    <tbody id="tb">
    <tr>
        <td><input type="checkbox"></td>
        <td>1.1.1</td>
        <td>80</td>
    </tr>
    <tr>
        <td><input type="checkbox"></td>
        <td>1.1.1</td>
        <td>80</td>
    </tr>
    <tr>
        <td><input type="checkbox"></td>
        <td>1.1.1</td>
        <td>80</td>
    </tr>
   
    </tbody>
</table>
<script src="jquery-3.3.1.js"></script>
<script>
    function checkAll() {
        //prop('checked',true)设置选中状态
        $(':checkbox').prop('checked',true);
    }
    function cancelAll() {
        $(':checkbox').prop('checked',false);
    }
    function reverseAll() {
        $(':checkbox').each(function(){
            // 方法一:
            // if(this.checked){
            //     this.checked = false;
            // }else {
            //     this.checked = true;
            // }


            //方法二:
            //prop('checked')获取选中状态
            // if($(this).prop('checked')){
            //     $(this).prop('checked',false)
            // }else {
            //      $(this).prop('checked',true)
            // }

            //方法三:
            var v= $(this).prop('checked')?false:true
            $(this).prop('checked',v)

        })
    }
</script>
</body>
</html>

相关文章

网友评论

      本文标题:jQuery之checkbox全选、反选、取消

      本文链接:https://www.haomeiwen.com/subject/ihefwftx.html