简体   繁体   English

在javascript中取消确认功能提交表单

[英]cancel in confirm function submitting form in javascript

Got a form that requires users to input an amount to donate. 得到了一张表格,要求用户输入捐款金额。 On clicking submit, a function is called and the function is meant to display the amount specified and prompt the user to confirm if the amount typed is the actual amount or not. 单击提交后,将调用一个函数,该函数将显示指定的金额并提示用户确认键入的金额是否为实际金额。

The Cancel option in the Confirm() keeps submitting the form instead of returning false. Confirm()中的“取消”选项将继续提交表单,而不是返回false。

function donationFormSend(){

    get_donation_amount = document.getElementById("get_donation_amt").value;

    if(get_donation_amount != ''){
        return confirm("You have specified "+get_donation_amount+" as the amount you wish to donate. \n\n Are you sure you want to proceed with the donation?");
    }
    else{
        alert("Amount must be specified to process your donation.");
        return false;   
    }

}    


<form method="post" action="">
<div>
      <div>Donation Amount:</div>
      <input name="amount" type="text" id="get_donation_amt" required="required" />
    </div>   
  <input name="donation_submit" type="submit" id="Submit" value="Proceed" onclick="return donationFormSend();" />
  </form>

Jsfiddle link jsfiddle链接

Would be pleased getting help with this. 很高兴获得与此相关的帮助。

I updated your jsfiddle so it's in the expected format (loading the js in the head) and returning the confirm result 更新了您的jsfiddle ,使其处于预期格式(将js加载到头部)并返回确认结果

return confirm('blah blah')

works perfectly well for me in FF! 在FF中对我来说效果很好! Just make sure you clear your cache and reload your page. 只要确保您清除缓存并重新加载页面即可。

A way to do do it might be: 一种实现方法可能是:

form : 形式:

   <form id='test' method="post" action="">
    <div>
          <div>Donation Amount:</div>
          <input name="amount" type="text" id="get_donation_amt" required="required" />
     </div>   
      <input type="submit" value="submit" onClick="form_submit(this.value)">
    <input type="submit" value="cancel" onClick="form_submit(this.value)">
     </form>

javascript: javascript:

document.getElementById('test').addEventListener('submit',function (event) {
 if (event.preventDefault) { 
      event.preventDefault();
   } else {
      event.returnValue = false; 
   }
})

form_submit= function (submited_by) {

  if(submited_by == 'submit'){
    alert('Submited')
   }else if (submited_by == 'cancel'){
   alert('cancelled')  
  }

}  

I'd rather use a switch statement to make it expandable in the future but this should work. 我宁愿使用switch语句使它将来可扩展,但这应该可行。

Also I'm using jquery mostly because I'm not sure how to stop default action without it. 另外,我之所以使用jquery,主要是因为我不确定如果没有它,如何停止默认操作。

here's a JSFiddle with the code running. 这是运行代码的JSFiddle

EDIT: Updated to not use Jquery. 编辑:更新为不使用Jquery。

EDIT: well, I feel stupid now, realised it wasn't cancel button in a submit form but in a confirmation form. 编辑:好吧,我现在觉得很愚蠢,意识到这不是在提交表单中而是在确认表单中取消按钮。

In your HTML use :   onclick="return  donationFormSend();"
In Your Javascript:  return confirm("Are you sure ....blah blah blah")

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

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