【问题标题】:form disabled on disable button and change text在禁用按钮上禁用表单并更改文本
【发布时间】:2012-04-01 23:29:08
【问题描述】:

我正在尝试禁用单击按钮,以及更改按钮的文本。这是我的代码:

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

发生了什么,按钮被禁用,文本发生变化,但表单没有做任何事情(提交)。我做错了什么?

【问题讨论】:

    标签: javascript forms button


    【解决方案1】:

    这行得通:

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

    【讨论】:

    • 你确定吗?因为它似乎对此不起作用: if(isset($_POST['submit1'])){
    【解决方案2】:

    具有名称的表单控件可作为其所在表单的命名属性使用其名称。所以:

    document.form1.submit
    

    指的是按钮,而不是提交方法。

    写作:

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

    表示“javascript”被视为无用标签,请不要这样做。如果您希望在提交表单时禁用按钮并更改其标签,请使用以下内容:

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

    并让表单正常提交。

    当然,onclick 属性中的函数应该是一个函数调用,而不是一段代码,但你明白了。

    【讨论】:

      猜你喜欢
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      • 2011-06-24
      • 1970-01-01
      • 2019-05-14
      • 1970-01-01
      相关资源
      最近更新 更多