douyong1908 2018-05-24 05:20
浏览 419
已采纳

选中复选框时,调用Ajax或提交按钮

I have following PHP while loop which is showing data as the checkbox. The checkbox can be multiple or one.

<?php
$supplier = new Admin;
$supplier->rowQuery("SELECT * FROM supplier");

echo '<form id="formId">';
    while ( $data = $supplier->result->fetch_assoc()) {        

        $sid          =  (int) $data['sid'];
        $supplierName = output($data['supplierName']);

        echo '<label class="checkbox-inline">';
            echo "<input type='checkbox' name='sid[]' data-sid='$sid' value='$sid' class='supplierClass'> $supplierName";
        echo '</label>';
    }
echo '<input type="hidden" name="submit" value="supplier" class="clickBtn">';
echo '</form>';
?>

Now I want to call Ajax when at least one checkbox or multiple checkbox is checked. So to do that I have following Javascrpt:

$('.supplierClass').click(function() {
var sid = $(this).data('sid');
if(this.checked){
   // $("input[type=submit]").trigger(".clickBtn");
   $('.clickBtn').trigger();
   $('#formId').submit(function() {
      $.ajax({
        url         :   'process/get-vehicle.php',
        type        :   'POST',
        dataType    :   'html',
        data        :   {
          sid   :  sid,
        },
        beforeSend  :   function () {
            $('.allVehicle').html('Please wait...');
        },
        success     :   function ( result ) {
            $('.allVehicle').html(result);
        }
    });
  });
}
});

but it's not called the Ajax :(

Overall Goal

You can see that I have set the checkbox name as array sid[]. Becuase I need to get all checkbox checked data as array in a PHP file.

So how can I call Ajax when one or multiple checkboxes are checked? Please note that: In PHP file I want to get all checkbox or one checkbox value.

My Thought:

I think there should be a submit button as hidden. When I click on any checkbox It's should press the hidden button by jQuery to call the Ajax.

  • 写回答

3条回答 默认 最新

  • dourong6054 2018-05-24 05:31
    关注

    Remove the submit event, use a change event, get all the checked sids

    $('.supplierClass').change(function() {
    
    if( $('.supplierClass:checked').length){
     var sids = [];
     $('.supplierClass:checked').each(function(i,v) {//get all the sids of the elements checked
         sids.push($(v).val());
      });
          $.ajax({
            url         :   'process/get-vehicle.php',
            type        :   'POST',
            dataType    :   'html',
            data        :   {
              sid   :  sids,
            },
            beforeSend  :   function () {
                $('.allVehicle').html('Please wait...');
            },
            success     :   function ( result ) {
                $('.allVehicle').html(result);
            }
        });
    }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog