Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-使用html5表单验证的一键调用ajax和一键提交表单_Javascript_Ajax - Fatal编程技术网

Javascript-使用html5表单验证的一键调用ajax和一键提交表单

Javascript-使用html5表单验证的一键调用ajax和一键提交表单,javascript,ajax,Javascript,Ajax,我有一个表单,我想使用required属性,我相信它来自html5,以确保用户在运行ajax发送和发送电子邮件之前输入一个名称,并停留在这个页面上(index.php)。下面的表格有效。我的问题是,我不知道如何使用一个名为pay的按钮将表单提交到pay.php,就像常规表单提交一样,因此用户在单击“pay”时会在pay.php上结束,我希望表单验证在单击pay时仍然发生,而在pay.php上,我可以从帖子中获取联系人姓名 <form id="contactForm" m

我有一个表单,我想使用required属性,我相信它来自html5,以确保用户在运行ajax发送和发送电子邮件之前输入一个名称,并停留在这个页面上(index.php)。下面的表格有效。我的问题是,我不知道如何使用一个名为pay的按钮将表单提交到pay.php,就像常规表单提交一样,因此用户在单击“pay”时会在pay.php上结束,我希望表单验证在单击pay时仍然发生,而在pay.php上,我可以从帖子中获取联系人姓名

<form id="contactForm" method="post" class="tm-contact-form">                                
  Name: <input type="text" id="contactName" name="contactName" class="form-control" placeholder="Name" required/>
  <button type="submit" id="inquire-button"  class="btn btn-primary">Inquire</button>
  <div id="mail-status">&nbsp;</div>
</form> 

<script type="text/javascript">
$("inquire-button").on('click',function(e){
  e.preventDefault();
});

$("#contactForm").on('submit',function(e){
  sendContact();
  e.preventDefault();
});

function sendContact() {
  jQuery.ajax({
  url: "mailer.php",
  data:'contactName='+$("#contactName").val(),
  type: "POST",
  success:function(data){
    $("#mail-status").html(data);
  },
    error:function (){}
  });
}
</script>

姓名:
询问
$(“查询按钮”)。在('click',函数(e){
e、 预防默认值();
});
$(“#contactForm”)。在('submit',函数(e)上{
sendContact();
e、 预防默认值();
});
函数sendContact(){
jQuery.ajax({
url:“mailer.php”,
数据:'contactName='+$(“#contactName”).val(),
类型:“POST”,
成功:功能(数据){
$(“#邮件状态”).html(数据);
},
错误:函数(){}
});
}

也不要使用单击,而只使用提交事件

$(“#contactForm”)。关于('submit',函数(e){
const$btn=$(document.activeElement);
如果($btn.is(“#查询”)){
console.log(“单击查询”)
e、 预防默认值();
jQuery.ajax({
url:“mailer.php”,
数据:“contactName=”+$(“#contactName”).val(),
类型:“POST”,
成功:功能(数据){
$(“#邮件状态”).html(数据);
},
错误:函数(){}
});
}
else console.log(“点击支付”)
})

姓名:
询问
支付

也不要使用单击,而只使用提交事件

$(“#contactForm”)。关于('submit',函数(e){
const$btn=$(document.activeElement);
如果($btn.is(“#查询”)){
console.log(“单击查询”)
e、 预防默认值();
jQuery.ajax({
url:“mailer.php”,
数据:“contactName=”+$(“#contactName”).val(),
类型:“POST”,
成功:功能(数据){
$(“#邮件状态”).html(数据);
},
错误:函数(){}
});
}
else console.log(“点击支付”)
})

姓名:
询问
支付

谢谢!!!!第二个建议对我有效。但只是出于好奇。。。第一个没有。在您的第一个建议中,如何获取要发布到pay.php的表单?如果我在表单标签中添加action=pay.php,那么它会这样做,但另一个按钮不会像我所希望的那样停留在同一页面上。谢谢!!!!第二个建议对我有效。但只是出于好奇。。。第一个没有。在您的第一个建议中,如何获取要发布到pay.php的表单?如果我将action=pay.php添加到表单标记中,那么它会这样做,但另一个按钮不会像我希望的那样停留在同一页面上。