Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当使用jQuery更改复选框属性时,模拟单击多个复选框以发出AJAX请求_Javascript_Jquery_Forms_Checkbox - Fatal编程技术网

Javascript 当使用jQuery更改复选框属性时,模拟单击多个复选框以发出AJAX请求

Javascript 当使用jQuery更改复选框属性时,模拟单击多个复选框以发出AJAX请求,javascript,jquery,forms,checkbox,Javascript,Jquery,Forms,Checkbox,我有一个矩阵样式的网格,允许在每个用户的基础上更新项目的一些用户权限 它使用一个表格网格,并发出一个AJAX请求,以便在单击时立即更新每个设置 我刚刚添加了一些jQuerycode到批量检查中,并取消选中用户行的所有复选框 我遇到的问题是,当一行被选中时,我需要让它同时对所有这些复选框发出AJAX请求 下面是演示和JSFIDLE演示的所有代码 jsiddle演示: 预览图像: JavaScript/jQuery: $(document).ready(function() { //

我有一个矩阵样式的网格,允许在每个用户的基础上更新项目的一些用户权限

它使用一个表格网格,并发出一个AJAX请求,以便在单击时立即更新每个设置

我刚刚添加了一些
jQuery
code到批量检查中,并取消选中用户行的所有复选框

我遇到的问题是,当一行被选中时,我需要让它同时对所有这些复选框发出AJAX请求

下面是演示和JSFIDLE演示的所有代码

jsiddle演示:


预览图像:


JavaScript/jQuery:

$(document).ready(function() {

    // Check/Un-check All checkboxes in a User Row
    $(".checkall").click(function(){
        $(this).parents('tr').find(':checkbox').prop('checked', this.checked);
    });

    // Make AJAX Request to Update User Permission setting in Backend Database
    $(".flipswitch").change(function () {
        var flip = $(this).closest('td');
        //alert("perm_id="+this.value+"&permissions=" + this.checked);
        $.ajax({
            type: 'post',
            url: '<?php echo $_SERVER['PHP_SELF']; ?>',
            data: "perm_id="+ this.value + "&permission=" + this.checked,
            success: function() {
                flip.effect("highlight", {color:"#12D812"}, 2000)
            }
        });
    });

});
<table>
    <tbody>
        <tr class="table_header">
            <th>&nbsp;</th>
            <th align="center">https://google.com/-1</th>
            <th align="center">https://google.com/-2</th>
            <th align="center">https://google.com/-3</th>
            <th align="center">https://google.com/-4</th>
            <th align="center">https://google.com/-5</th>
        </tr>

        <tr>
            <td><input class="checkall" type="checkbox"> user 1-1</td>
            <td align="center">user 1<input checked="checked" class=
            "flipswitch" name="0" type="checkbox" value="1">1</td>
            <td align="center">user 1<input checked="checked" class=
            "flipswitch" name="1" type="checkbox" value="2">1</td>
            <td align="center">user 1<input checked="checked" class=
            "flipswitch" name="2" type="checkbox" value="3">1</td>
            <td align="center">user 1<input checked="checked" class=
            "flipswitch" name="3" type="checkbox" value="4">1</td>
            <td align="center">user 1<input checked="checked" class=
            "flipswitch" name="4" type="checkbox" value="5">1</td>
        </tr>

        <tr>
            <td><input class="checkall" type="checkbox"> user 2-2</td>
            <td align="center">user 2<input checked="checked" class=
            "flipswitch" name="5" type="checkbox" value="6">2</td>
            <td align="center">user 2<input checked="checked" class=
            "flipswitch" name="6" type="checkbox" value="7">2</td>
            <td align="center">user 2<input checked="checked" class=
            "flipswitch" name="7" type="checkbox" value="8">2</td>
            <td align="center">user 2<input checked="checked" class=
            "flipswitch" name="8" type="checkbox" value="9">2</td>
            <td align="center">user 2<input checked="checked" class=
            "flipswitch" name="9" type="checkbox" value="10">2</td>
        </tr>

        <tr>
            <td><input class="checkall" type="checkbox"> user 3-3</td>
            <td align="center">user 3<input checked="checked" class=
            "flipswitch" name="10" type="checkbox" value="11">3</td>
            <td align="center">user 3<input checked="checked" class=
            "flipswitch" name="11" type="checkbox" value="12">3</td>
            <td align="center">user 3<input checked="checked" class=
            "flipswitch" name="12" type="checkbox" value="13">3</td>
            <td align="center">user 3<input checked="checked" class=
            "flipswitch" name="13" type="checkbox" value="14">3</td>
            <td align="center">user 3<input checked="checked" class=
            "flipswitch" name="14" type="checkbox" value="15">3</td>
        </tr>

        <tr>
            <td><input class="checkall" type="checkbox"> user 4-4</td>
            <td align="center">user 4<input checked="checked" class=
            "flipswitch" name="15" type="checkbox" value="16">4</td>
            <td align="center">user 4<input checked="checked" class=
            "flipswitch" name="16" type="checkbox" value="17">4</td>
            <td align="center">user 4<input checked="checked" class=
            "flipswitch" name="17" type="checkbox" value="18">4</td>
            <td align="center">user 4<input checked="checked" class=
            "flipswitch" name="18" type="checkbox" value="19">4</td>
            <td align="center">user 4<input checked="checked" class=
            "flipswitch" name="19" type="checkbox" value="20">4</td>
        </tr>

        <tr>
            <td><input class="checkall" type="checkbox"> user 5-5</td>
            <td align="center">user 5<input checked="checked" class=
            "flipswitch" name="20" type="checkbox" value="21">5</td>
            <td align="center">user 5<input checked="checked" class=
            "flipswitch" name="21" type="checkbox" value="22">5</td>
            <td align="center">user 5<input checked="checked" class=
            "flipswitch" name="22" type="checkbox" value="23">5</td>
            <td align="center">user 5<input checked="checked" class=
            "flipswitch" name="23" type="checkbox" value="24">5</td>
            <td align="center">user 5<input checked="checked" class=
            "flipswitch" name="24" type="checkbox" value="25">5</td>
        </tr>

        <tr>
            <td><input class="checkall" type="checkbox"> user 6-6</td>
            <td align="center">user 6<input checked="checked" class=
            "flipswitch" name="25" type="checkbox" value="26">6</td>
            <td align="center">user 6<input checked="checked" class=
            "flipswitch" name="26" type="checkbox" value="27">6</td>
            <td align="center">user 6<input checked="checked" class=
            "flipswitch" name="27" type="checkbox" value="28">6</td>
            <td align="center">user 6<input checked="checked" class=
            "flipswitch" name="28" type="checkbox" value="29">6</td>
            <td align="center">user 6<input checked="checked" class=
            "flipswitch" name="29" type="checkbox" value="30">6</td>
        </tr>
    </tbody>
</table>
table {  border: 1px solid #000;  }
th {  background-color: #CCC;  }
td {  border: 1px solid #CCC;   }
我想出来了

我添加了一个调用来发出/触发一个
change

$(this).parents('tr').find(':checkbox').change();
所以更新的jQuery代码是

$(document).ready(function() {

    $(".checkall").click(function(){
        $(this).parents('tr').find(':checkbox').prop('checked', this.checked);
        $(this).parents('tr').find(':checkbox').change();
    });


    $(".flipswitch").change(function () {
        var flip = $(this).closest('td');
        //alert("perm_id="+this.value+"&permissions=" + this.checked);
        $.ajax({
            type: 'post',
            url: '/echo/html/',
            data: "perm_id="+ this.value + "&permission=" + this.checked,
            success: function() {
                flip.effect("highlight", {color:"#12D812"}, 2000)
            }
        });
    });

});
当现在单击批量更新行的复选框时,更新的JSFIDLE会发出AJAX请求X列数。因此,当一行中的所有复选框都被选中和取消选中时,每个复选框都会有一个AJAX请求

希望这能帮助其他人。我发布了这个答案,而不是删除这个问题,以备将来个人参考

如果有更好的办法,我当然也愿意!谢谢


更新

根据@charlietfl的回答,最好将我的
change()
附加到当前jQuery元素搜索/查找的末尾,而不是进行第二次查找以查找复选框

$(this).parents('tr').find(':checkbox').prop('checked', this.checked).change();

将一次触发大量请求,但您可以尝试的第一件事是在设置
checked
属性时触发
change
事件

$(".checkall").click(function(){
        $(this).parents('tr')
               .find(':checkbox')
               .prop('checked', this.checked)
               .change();// trigger change event
});

谢谢,我在发帖后就发现了这个。不过,我喜欢只查找一次物品的想法+1如果时间允许,我会接受你的回答,谢谢