php 我如何让 AJAX 使用多个复选框值,而不仅仅是第一个?

niknxzdl  于 10个月前  发布在  PHP
关注(0)|答案(1)|浏览(76)

我正在创建一个用户管理页面,其中我有一个表,表的第一列是复选框,第一列是一个全选框。我想一个按钮采取的值或值,如果复选框被勾选,并发送后请求。我写的代码,但它总是并且只发送表中的第一个值,无论任何框是否票。
HTML表格如下:

<table id="userList" class="table table-bordered table-striped">
            <thead>
                <tr>
                    <th><input type="checkbox" onClick="toggle(this)" /></th>
                    <th>Name</th>
                    <th>Credential</th>
                    <th>Email</th>
                    <th>Start Date</th>
                    <th>End Date</th>               
                    <th>Status</th>
                    <th></th>
                    <th></th>
                    <th></th>
                    

                </tr>
            </thead>
        </table>

字符串
第一列是从php代码填充的复选框
第一个月
复选框选择所有脚本

function toggle(source) {
    checkboxes = document.getElementsByName('check');
    for(var i=0, n=checkboxes.length;i<10;i++) {
      checkboxes[i].checked = source.checked;
    }
  }


我想做一个按钮,以获取userid值,并提交他们在发布请求,以禁用多个用户在同一时间php代码工作正常,但我无法使jquery部分工作:

$(document).on('click', '.deactivateUsers', function(){
        var userid = $('#cbuserid').val();  
        var action = "userDeactivate";
        if(confirm("Are you sure you want to deactivate this user?")) {
            $.ajax({
                url:"action.php",
                method:"POST",
                data:{userid:userid, action:action},
                success:function(data) {                    
                    window.location.reload();
                    
                }
            })
        } else {
            return false;
        } 
    });


无论是否勾选了任何复选框,这段代码总是取cbuserid的第一个值。

iqih9akk

iqih9akk1#

我建议您使用按钮的onclick属性并添加

data-user="'.$users['id'].'"

字符串
类似这样的东西::

<input type="button" onclick="function_name()" data-user="'.$users['id'].'">
function function_name()
{
  let id = $(this).attr("data-user");
}


或无数据

<input type="button" onclick="function_name("'.$users['id'].'")">
function function_name(user_id)
{
  let id = user_id;
}


您可以使用数据属性的方法

<input class="checkbox-user" type="checkbox" data-user="'.$users['id'].'"/>

$('input:checkbox:checkbox-user').each(function () {
    if(this.checked)
        console.log($(this).attr("data-user"););
});

相关问题