简体   繁体   English

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

[英]Javascript - one button call ajax and one button submit form using html5 form validation

I have a form that I want to use the required attribute, which I believe is from html5 to make sure the user puts in a name before running the ajax to send and email and stays on this page (index.php).我有一个表单,我想使用 required 属性,我相信它来自 html5,以确保用户在运行 ajax 发送和发送电子邮件之前输入一个名称并停留在此页面 (index.php) 上。 The form below works.下面的表格有效。 My problem is that I can't figure out how to aa button called pay that submits the form to pay.php like a regular form submit so the user ends up on pay.php when they click "pay" and I want the form validation to still occur when they click pay and on pay.php I can grab the contactName from the post.我的问题是,我无法弄清楚如何使用一个名为 pay 的按钮将表单提交到 pay.php,就像常规表单提交一样,因此用户在单击“支付”时最终会使用 pay.php 并且我想要表单验证当他们点击 pay 和 pay.php 时仍然会发生,我可以从帖子中获取 contactName 。

<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>

EITHER don't use the click, but only the submit event要么不使用点击,而只使用提交事件

 $("#contactForm").on('submit', function(e) { const $btn = $(document.activeElement); if ($btn.is("#inquire")) { console.log("Inquire clicked") e.preventDefault(); jQuery.ajax({ url: "mailer.php", data: 'contactName=' + $("#contactName").val(), type: "POST", success: function(data) { $("#mail-status").html(data); }, error: function() {} }); } else console.log("Pay clicked") })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <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> <button type="submit" id="pay" class="btn btn-primary">Pay</button> <div id="mail-status">&nbsp;</div> </form>

OR use a button或使用按钮

 $("#contactForm").on('submit', function(e) { console.log("Submitted (pay)") }) $("#inquire-button").on("click", function() { if (!this.form.checkValidity()) { this.form.reportValidity() return } console.log("Inquire clicked") jQuery.ajax({ url: "mailer.php", data: 'contactName=' + $("#contactName").val(), type: "POST", success: function(data) { $("#mail-status").html(data); }, error: function() {} }); })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <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="button" id="inquire-button" class="btn btn-primary">Inquire</button> <button type="submit" id="pay" class="btn btn-primary">Pay</button> <div id="mail-status">&nbsp;</div> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM