& # 39; onclick # 39;似乎没有调用'submit'按钮
'onclick' doesn't seem to call a function with 'submit' button
我已经遇到过很多次了(我不想在这里使用'input type button')为什么哦为什么'onclick'不调用函数check();? 即使字段为空,页面仍然提交。谢谢你的意见。
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>
所以…页面不应该在空字段上提交——为什么要提交呢?谢谢. .
把你的函数改成这样:
function check() {
var name = document.getElementById('username_res').value;
var passw_old = document.getElementById('passw_reset_old').value;
var passw_new = document.getElementById('passw_reset').value;
return ((name != '') && (passw_old != '') && (passw_new != ''));
}
你实际上不需要将onclick事件更改为onsubmit,只需将其更改为onclick="return check();"
。你需要给onclick事件返回false来阻止它被触发。
您可能需要添加一些视觉增强功能,以表明表单未提交的原因。否则会让访问者感到困惑。
你试图在一个javascript变量上调用jquery方法。
试试这个
if ((name.value == "") && (passw_old.value == "") && (passw_new.value == "")) {
return false;
} else {
return true;
}
提交按钮应该是
<input type="submit" value="submit" id="sub_res" name="sub_res" onclick="return check();"/>
您需要将按钮的类型从type="submit"更改为type="button",然后在check()函数中,如果一切顺利,则需要获取表单对象并提交它。
比如document。getelementbyid ('formID')。submit()您需要返回false
以避免事件传播。
喜欢这里。
你可以尝试使用表单的onSubmit函数
<form id="passw_res" action="pass_reset.php" onSubmit="check()">
页面提交,因为在您的check()
脚本中没有任何东西可以阻止它。您的脚本只在验证时禁用按钮,但此时单击/提交已经发生,因此,表单将提交。
因此使用onsubmit='return check();'
,而不是禁用,根据验证返回true或false。如果check()返回false,它将"停止"提交操作,如果返回true,它将继续提交。
相关文章:
- 如果从文件输入按钮调用 .ajaxSubmit() 时,它是必需的表单
- 使用函数按钮调用 ajaxform
- 使用 PHP 生成的按钮生成 POST 请求,这些按钮调用带有参数的 javascript 函数
- 如何从按钮调用LinkedIn授权javascript
- Yii2:用按钮调用 javascript 函数
- 点击按钮调用Jade mixin功能
- 如何点击按钮调用angularJs服务
- 未通过单击按钮调用JavaScript回调
- 动态按钮调用JavaScript函数
- 从javascript onclick事件按钮调用c函数
- 我用脚本创建了一个按钮.我可以通过点击按钮调用其他函数吗?我这样做,但失败了
- 如何使用HTML按钮调用javascript函数
- 单击Fancy Box覆盖按钮调用Ajax时,Ajax未按预期工作
- 使用 onClick 从 JavaScript 创建 html 按钮调用一个带有一个参数的方法
- Javascript 没有从 html 按钮调用
- 从新的 dom 按钮调用内容脚本函数
- 使用按钮调用 JavaScript 函数
- 如何对函数进行按钮调用
- ember - 控制器操作仅由输入字段调用,而不由按钮调用
- 如何从 ui-grid 中显示的按钮调用范围方法 - 在 Angular js 中