简体   繁体   English

如何在同一按钮上处理JavaScript操作并提交表单操作

[英]How to handle javascript action and submit form action on a same button

In my form, I am trying to run one javascript function to compare 2 textbox values. 在我的表单中,我试图运行一个javascript函数来比较2个文本框值。 also, In that same button, i am trying to perform submit action to save the form data to DB and redirect to next page. 另外,在同一按钮中,我试图执行提交操作以将表单数据保存到DB并重定向到下一页。 JS function works fine, it generates alert for wrong input, but as soon as I click on alert. JS函数工作正常,它会针对错误的输入生成警报,但是只要单击警报即可。 It is redirecting to next page without saving form data to DB. 它正在重定向到下一页,而没有将表单数据保存到DB。 Can anyone tell me how to stop this? 谁能告诉我如何制止这种行为?

I want to do: 我想要做:

  1. Compare 2 textbox values; 比较2个文本框值; if they are the same form should submit and save to DB(working) 如果它们是相同的表单,则应提交并保存到数据库(工作)
  2. If values are not matching then show alert(working), but it on clicking "ok" it should not redirect until the values are right for both textboxes. 如果值不匹配,则显示alert(正在工作),但是单击“确定”后,它将不会重定向,直到两个文本框的值都正确为止。

JSFiddle JSFiddle

 function check(){ var fistInput = document.getElementById("total").value; alert(fistInput); var secondInput = document.getElementById("amount").value; if(fistInput === secondInput) { alert("Good"); } else if(fistInput > secondInput) { alert("Please input correct value"); } else { alert("Please input correct value"); } } 
  <form class="form-horizontal" action="print-receipt.php" method="post" name="myform" autocomplete="off" > <input type="text" id="total" placeholder="Total Amount" name="total" required> <input type="text" id="amount" placeholder="Total" name="amount"> <button type="submit" name="submit" onclick="check()" >Submit</button> </form> 

Use event.preventDefault() when form data is invalid, to prevent the form from being submitted. 表单数据无效时,请使用event.preventDefault() ,以防止提交表单。

Instead of onclick on your type='submit' button, use onsubmit handler on your <from> element. 代替onclick您的type='submit'按钮,在您的<from>元素上使用onsubmit处理程序。 You can pass an event to your function like this: 您可以像这样将event传递给函数:

<form onsubmit='check(this)'></form>

 function check(event){ var fistInput = document.getElementById("total").value; alert(fistInput); var secondInput = document.getElementById("amount").value; if(fistInput === secondInput) { alert("Good"); } else if(fistInput > secondInput) { event.preventDefault(); alert("Please input correct value"); } else { event.preventDefault(); alert("Please input correct value"); } } 
  <form class="form-horizontal" action="print-receipt.php" method="post" name="myform" autocomplete="off" onsubmit="check(this)"> <input type="text" id="total" placeholder="Total Amount" name="total" required> <input type="text" id="amount" placeholder="Total" name="amount"> <button type="submit" name="submit" >Submit</button> </form> 

Try this: 尝试这个:

 var form = document.getElementById("f"); function check(){ var fistInput = document.getElementById("total").value; alert(fistInput); var secondInput = document.getElementById("amount").value; if(fistInput === secondInput) { alert("Good"); form.action = "print-receipt.php"; } else if(fistInput > secondInput) { alert("Please input correct value"); form.action = ""; } else { alert("Please input correct value"); form.action = ""; } } 
 <form id="f" class="form-horizontal" action="" method="post" name="myform" autocomplete="off"> <input type="text" id="total" placeholder="Total Amount" name="total" required> <input type="text" id="amount" placeholder="Total" name="amount"> <button type="submit" name="submit" onclick="check()" >Submit</button> </form> 

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

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