简体   繁体   中英

form disabled on disable button and change text

I am trying to disable a button on click, as well as change the text of the button. here is my code:

<input type="submit" value="Register" name="submit" id="submit" onClick="javascript:replaceButtonText('submit', 'Please wait...'); document.form1.submit.disabled=true;">

What is happening, is the button gets disabled, and the text changes, but the form does not do anything (submit). what am I doing wrong?

This works:

<html>
<body>
    <form name="form1" method="post" action="myaction">
        <input type="text" value="text1"/>

        <input type="submit" value="Register" name="submit" id="submit" 
            onclick="javascript: replaceButtonText('submit1', 'Please wait...'); document.form1.submit.disabled=true; return true; ">
    </form>

</body>
</html>

Form controls with a name are made available as named properties of the form they are in using their name. So:

document.form1.submit

refers to the button, not the submit method.

Writing:

< ... onclick="javascript:..." ...>

means that "javascript" is treated as a useless label, just don't do it. If you want the button to become disabled and change its label when the form is submitted, then use something like:

<form>
  <input name=foo value=bar>
  <input type="submit" onclick="
    this.value='Please wait...';
    this.disabled = true;
    var theForm = this.form;
    window.setTimeout(function(){theForm.submit();},1);
">
</form>

and let the form submit normally.

Of course the function in the onclick attribute should be a function call rather than a slab of code, but you get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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