我已經閱讀了幾個關於這個問題的SO帖子和我發現的一個可能的解釋是this之一。 該帖子中的回覆說「如果以編程方式調用submit(),瀏覽器不會調用窗體上的onSubmit事件。」my onsubmit =「return onNewUserRegistrationCheck(this);」仍然提交表格上的錯誤
我不確定這是否影響我的表格。
我的代碼onsubmit =「return onNewUserRegistrationCheck(this);」是100%工作 - 對onNewUserRegistrationCheck(this)的調用是100%發生。
下面是代碼:
function onNewUserRegistrationCheck(theForm)
{
alert("In onNewUserRegistrationCheck()");
if(theForm.loginname.value == "<?php echo Labels::$DEFAULTLOGINEMAILLABEL ?>"
|| theForm.password.value == "<?php echo Labels::$DEFAULTPASSWORDLABEL ?>")
{
alert("returning false now, so -- HOPEFULLY the form doesn't submit");
return false;
}
}
這裏的形式:
<form enctype="multipart/form-data" onsubmit="return onNewUserRegistrationCheck(this);"
class="registerFormStyles" name="registerForm" id="regform"
action="regTheNewUser.php" method="post">
<input type="text" id="loginname" name="<?php echo Labels::$USERNAMELABEL ?>"
style="width:260px" value="<?php echo Labels::$DEFAULTLOGINEMAILLABEL ?>" />
<input type="password" placeholder="<?php echo Labels::$DEFAULTPASSWORDLABEL ?>"
id="password" name="<?php echo Labels::$PASSWORD ?>"
style="width:220px" class="inactive" />
<input type="hidden" name="newUserRegister" id="newUserRegisterId">
<input type="image" name="registerButton" value="Register" class="login-button-landing"
onclick="document.registerForm.submit();">
</form>
通知我使用類型= 「形象」,做一個綱領性提交()。據一些SO職位的 結果的程序提交()像我這樣做是這樣的:「瀏覽器不如果調用提交(以編程方式調用窗體上的onSubmit事件)」
不過我「M看到2個警告框,當我在我的名字點擊=‘registerButton’僞提交按鈕:
alert box #1 pops up and says: "In onNewUserRegistrationCheck()"
然後....
alert box #2 pops up and says: "returning false now, so -- HOPEFULLY the form doesn't submit"
這2個警告框出現,當我點擊我的名字=「registerButton」bu在窗體上的tton。 所以我必須(?)誤解我讀過的帖子中的含義,如 '調用submit()不會觸發onsubmit事件'。
底線是仍然提交文件「regTheNewUser.php」 即使你可以看到,由於被視爲「現在返回false警報消息 的「返回false」發生,所以形式 - HOPEFULLY表格未提交「
如何使用type =」submit「按鈕停止提交的表格而不使用? 我向自己證明我的onsubmit =「return onNewUserRegistrationCheck(this);」 被調用100%。我返回false,但無論如何都提交表單。
當我發現無效的表單文本條目 以及爲什麼上面沒有'返回false'停止我的表單提交時,如何停止提交的表單?
你可以嘗試'onsubmit =「onNewUserRegistrationCheck(this);返回false;」'只是爲了看看它是否工作。如果是這樣,至少你知道這是你的函數邏輯的問題。 – Jacksonkr 2012-03-03 06:00:51
會嘗試。然而看看上面的代碼,alert(「現在返回false,所以 - HOPEFULLY表單不提交」); 返回false; - 我不會看到'HOPEFULLY..etc',也不會返回false。我看到'HOPEFULLY .....'所以'return false'必須發生 - 如果你看代碼,似乎沒有辦法讓'HOPEFULLY ...'出現,除非發生'return false'之後。 – wantTheBest 2012-03-03 06:07:36