[英]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: 我想要做:
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.