关于javascript的表单提交问题。 [英] form submission issue with respect to javascript.
问题描述
我遇到一个问题,表单似乎要回发两次。一旦我单击提交按钮,然后在页面的警报中单击确定按钮后再次。我认为它与表单标签中的提交按钮和onsubmit设置有关。
表单标签中的onsubmit正在调用一个名为checkform()的函数,该函数检查数据的输入字段,如果没有,则在其旁边放置一个标记。一种基本的验证。
这是我使用的基本代码。如果有人能提出更好的方法或解决方案,我将不胜感激。谢谢。
I have an issue where a form appears to be posting back twice. Once when I click the submit button, then once again after the "ok" button is clicked in an alert from the page. I am thinking it has to do with there being a submit button as well as an onsubmit set in the form tag.
The onsubmit in the form tag is calling a function called checkform() which checks the input fields for data and puts a mark next to it if not. Sort of a rudimentary validation.
Here is the basic code I am using. If anyone can suggest a better way or a solution, I would appreciate it. Thank you.
<script language="JavaScript" type="text/javascript">
function checkform(form) {
var isformvalid = true;
// ** START **
if (document.forms["EnrollMyInventory"].elements["Dealer Name"].value == "") {
document.getElementById('dnError').innerHTML = "*"
isformvalid = false;
}
else {
document.getElementById('dnError').innerHTML = ""
}
if (document.forms["EnrollMyInventory"].elements["Dealer Number1"].value == "") {
document.getElementById('dNumError').innerHTML = "*"
isformvalid = false;
}
else {
document.getElementById('dNumError').innerHTML = ""
}
return isformvalid;
}
</script>
<form method="post" action="enrollmyinventorycopy2.asp" name="EnrollMyInventory" onsubmit="return checkform(this);" >
<span class="style3">Dealer Name: </span>
<span class="style3">
<input type="text" name="Dealer Name" value="<%=Request.Form("Dealer Name")%>"
size="33"><label style="color:Red" id="dnError"></label>
<br>
Dealer Number:
<input type="text" name="Dealer Number1" value="<%=Request.Form("Dealer Number1")%>"
size="33"><label style="color:Red" id="dNumError"></label>
<input type="submit" name="Submit" value="Submit" />
</form>
推荐答案
我最终搞清楚了解决方案。
我没有使用form标签内的onsubmit和submit类型输入,而是使用常规按钮并使用onclick调用checkform()函数。在checkform()函数中,我调用.submit()在验证发生后提交表单。
I ended up figuring out the solution.
Instead of using the "onsubmit" inside the form tag and an "submit" type input, I used a regular button and called the checkform() function with the onclick. Inside the checkform() function, I called .submit() to submit the form after the validation occurs.
这篇关于关于javascript的表单提交问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!