【问题标题】:get array id value from checkbox to jquery function从复选框获取数组 id 值到 jquery 函数
【发布时间】:2019-07-29 08:09:37
【问题描述】:

我想在复选框中获取 id 的值。但是 id 中的变量总是给出第一个值。如何将 php 数组发送到 jquery 数组。谢谢

require "connection.php";
$sql = mysqli_query($con,"SELECT * FROM position ORDER BY position_align + 0 ASC");
$switch = array();
$pid = array();
while($row = mysqli_fetch_assoc($sql)){
    $pcode  = $row['position_code'];
    $pdesc  = $row['position_desc'];
    $status = $row['status'];
    ?>
    <tr>
        <td><?php echo $pcode; ?></td>
        <td><?php echo $pdesc; ?></td>
        <td>
            <div class="custom-control custom-switch">
                <?php
                if($status == 'active'){
                ?>
                <div class="onoffswitch4">
                  <input type="checkbox" name="onoffswitch4" class="onoffswitch4-checkbox switch" onclick="change(this.value)" id="myonoffswitch" checked value="<?php echo $pcode; ?>">
                  <label class="onoffswitch4-label" for="pid[]">
                      <span class="onoffswitch4-inner"></span>
                      <span class="onoffswitch4-switch"></span>
                  </label>
                </div>

                <?php
                }else{
                ?>
                <div class="onoffswitch4">
                    <input type="checkbox" name="onoffswitch4" class="onoffswitch4-checkbox switch" onclick="change(this.value)" id="aaa[]" value="<?php echo $pcode; ?>">
                    <label class="onoffswitch4-label" for="myonoffswitch">
                        <span class="onoffswitch4-inner"></span>
                        <span class="onoffswitch4-switch"></span>
                    </label>
                </div>
                <?php
                }
                ?>

$('.switch').on('change', function() {
    if(this.checked) {
        var id = this.value;
        $.ajax({
            url: "admin_change_status.php",
            data: {
                id:id
            },
            type: "POST",
            success: function(result){

            }
        });
    }else{
        var id2 = this.value;
        $.ajax({
            url: "admin_change_status.php",
            data: {
                id2:id2
            },
            type: "POST",
            success: function(result){
            }
        });
    }
});

我希望将复选框中 id 的值传递给 jquery。

【问题讨论】:

  • 您想在选中或所有值时获取复选框值?

标签: php jquery arrays


【解决方案1】:

如果要获取所有值,只能通过类名获取,不能通过id获取。您必须遍历所有类名称并获取它们中的每一个,您可以通过每个函数执行此操作。每个函数循环遍历所有元素。

$('.onoffswitch4-checkbox').each(function(i){
    var statsValue = $(this).val(); 

    console.log(statsValue);
});

【讨论】:

  • 一旦你存储了statsValue,你需要用ajax传递它,期望你只需要传递一个值
猜你喜欢
  • 1970-01-01
  • 2015-02-28
  • 2019-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-22
相关资源
最近更新 更多