简体   繁体   English

'onclick'似乎没有使用'submit'按钮调用函数

[英]'onclick' doesn't seem to call a function with 'submit' button

I've come across this many times ( I don't want to use 'input type button' here ) why oh why the 'onclick' doesn't call the function check(); 我已经遇到过很多次了(我不想在这里使用“输入类型按钮”),为什么哦,为什么“ onclick”不调用函数check(); ? The page still submits even when the fields are empty. 即使字段为空,页面仍将提交。 Thank you for your comments. 谢谢您的意见。

Javascript: 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: 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>

So...the page shouldn't submit on empty fields - why does it submit ? 所以...页面不应在空白字段中提交-为什么要提交? Thanks.. 谢谢..

Change your function to this: 将功能更改为此:

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 != ''));

}

You actually don't have to change the onclick event to an onsubmit, just change it to onclick="return check();" 实际上,您不必将onclick事件更改为onsubmit,只需将其更改为onclick="return check();" . You need to return false to the onclick event to prevent it from being fired. 您需要向onclick事件返回false,以防止触发该事件。

You might want to add some visual enhancements to indicate why the form isn't submitted. 您可能需要添加一些视觉增强功能,以指示为什么不提交表单。 Otherwise it will be confusing to the visitor. 否则会使访问者感到困惑。

You're trying to call a jquery method on a stand javascript variable. 您正在尝试在标准javascript变量上调用jquery方法。

Try this instead 试试这个

if ((name.value == "") && (passw_old.value == "") && (passw_new.value == "")) {
    return false;  
} else { 
    return true; 
}

And you submit button should be 和您提交按钮应该是

<input type="submit" value="submit" id="sub_res" name="sub_res" onclick="return check();"/>

You need to change the type of the button from type="submit" to type="button" and then in the check() function if everything passes you need to get the form object and submit it. 您需要将按钮的类型从type =“ submit”更改为type =“ button”,然后在check()函数中,如果一切都通过了,则需要获取表单对象并提交。

So something like document.getElementById('formID').submit() 所以像document.getElementById('formID')。submit()

you need to return false to avoid event propagation. 您需要返回false以避免事件传播。

Like here . 喜欢这里

您可以尝试改用表单的onSubmit函数

<form id="passw_res" action="pass_reset.php" onSubmit="check()">

The page submit because there is nothing in your check() script that would stop it from. 该页面提交是因为您的check()脚本中没有阻止它的内容。 Your script only disables the button on validating, but by that time the click/submit has already happened and hence, the form would submit. 您的脚本仅禁用验证按钮,但是到那时,单击/提交已经发生,因此将提交表单。

So use onsubmit='return check();' 因此,使用onsubmit='return check();' , and instead of disabling, return a true or false based on your validation. ,而不是禁用它,而是根据您的验证返回true或false。 That would 'stop' the submit action if check() returned false, and would go ahead with the submit if it returns true. 如果check()返回false,则将“停止”提交操作,如果返回true,则将继续进行提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM