为什么我的按钮不使用ajax和jquery提交表单?

问题描述 投票:0回答:1

我试图理解为什么我的表格没有提交,但我不知道为什么。我试过console.log数组值的值,它完美地工作。

当我点击表单的提交按钮时,它只显示警告“ok”并重新加载页面,但不提交表单...

<form method="POST">
    <?php 
    while($service = $req->fetch()){ ?>

            <div class="form-control">
              <input type="checkbox" class="getValue" value="<?= $service['price']; ?>">

              <label><?= $service['service']; ?></label>
              <strong><?= $service['price']; ?>€</strong>
            </div>

    <?php } ?>

    <button type="button" name="submit" class="btn btn-warning" id="submit">Submit</button>
   </form>

$(document).ready(function(){
$('#submit').click(function(e){
    e.preventDefault(); // annul action par défaut du button

    var values = [];

    $('.getValue').each(function(){

        if($(this).is(":checked")){ 
            values.push($(this).val()); 
        }
    });

    values = values.toString();

    // PROBLEME HERE:
    $.ajax({
        url:"addServicesRequest.php",
        method:"POST",
        data:{values:values},
        success:function(data){
            alert("ok");
        }
    });
  });
});

非常感谢提前!

javascript jquery
1个回答
-2
投票

我有它的工作!错误是我放了而不是删除了e.preventDefault();

© www.soinside.com 2019 - 2024. All rights reserved.