Javascript 为什么我的按钮是';是否使用ajax和jquery提交表单?
我试图理解为什么我的表格没有提交,但我不知道为什么。 我尝试了console.log数组值的值,它工作得非常好 当我点击表单的提交按钮时,它只会显示“ok”警报并重新加载页面,但不会提交表单Javascript 为什么我的按钮是';是否使用ajax和jquery提交表单?,javascript,jquery,Javascript,Jquery,我试图理解为什么我的表格没有提交,但我不知道为什么。 我尝试了console.log数组值的值,它工作得非常好 当我点击表单的提交按钮时,它只会显示“ok”警报并重新加载页面,但不会提交表单 <form method="POST"> <?php while($service = $req->fetch()){ ?> <div class="form-control"> <inp
<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");
}
});
});
});
€
提交
$(文档).ready(函数(){
$(“#提交”)。单击(函数(e){
E.预防错误()
var值=[];
$('.getValue')。每个(函数(){
如果($(this).is(“:checked”){
value.push($(this.val());
}
});
values=values.toString();
//这里的问题是:
$.ajax({
url:“addServicesRequest.php”,
方法:“张贴”,
数据:{values:values},
成功:功能(数据){
警报(“正常”);
}
});
});
});
提前多谢 我让它工作了!错误是,我放置了而不是,并删除了e.preventDefault() 仅供参考:
e.preventDefault()代码>是无用的,因为带有type=button
的按钮没有任何需要防止的本机行为。但是,您是否打开了开发人员的控制台,查看了控制台选项卡上的错误和网络选项卡,以查看是否发出了请求?我已将其删除,谢谢@ScottMarcus表单仍未提交:(这是“网络”选项卡:尝试删除e.preventDefault?并可能将按钮类型设置为submit@Chris已经完成了,但仍然没有work@Chris否。如果将按钮类型设置为submit
,则需要e.preventDefault()
。