简体   繁体   English

表单禁用禁用按钮并更改文本

[英]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. 意味着“javascript”被视为无用的标签,只是不要这样做。 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. 当然onclick属性中的函数应该是一个函数调用而不是一块代码,但是你明白了。

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

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