【发布时间】:2011-09-19 05:23:34
【问题描述】:
我遇到过很多次(我不想在这里使用“输入类型按钮”)为什么哦为什么“onclick”不调用函数 check(); ?即使字段为空,页面仍会提交。谢谢你们的cmets。
Javascript:
function check() {
var name = document.getElementById('username_res');
var passw_old = document.getElementById('passw_reset_old');
var passw_new = document.getElementById('passw_reset');
var button_res = document.getElementById('sub_res');
if ((name.val() == "") && (passw_old.val().length == "") && (passw_new.val().length == "")) {
button_res.disabled = true;
} else {
button_res.disabled = false;
}
}
HTML:
<form id="passw_res" name="passw_res" method="post" action="pass_reset.php">
<fieldset style="width:300px"><br/>
<legend id="pass_legend" align="left">Password reset</legend><br/>
<input type="text" id="username_res" name="username_res" maxlength="50" />
<input type="password" id="passw_reset_old" name="passw_reset_old" maxlength="16"/>
<input type="password" id="passw_reset" name="passw_reset" maxlength="16" />
<input type="submit" value="submit" id="sub_res" name="sub_res" onclick="check();"/>
</fieldset>
</form>
所以...页面不应该在空字段上提交 - 为什么要提交?谢谢..
【问题讨论】:
-
有什么原因不能使用
onsubmit事件? -
使用
onsubmit。并在你的函数中return false;以防止在必要时提交。
标签: php javascript html