【问题标题】:Disable a h:commandButton when h:inputText has no value当 h:inputText 没有值时禁用 h:commandButton
【发布时间】:2013-05-18 13:30:44
【问题描述】:

我有两个 h:inputText 文本框和一个 h:commandButton。如果两个文本框中都没有值,我该如何禁用该按钮?另外,如果我在其中一个文本框中有值,我该如何禁用另一个文本框。请在下面找到我的示例代码:

<h:form id="myForm">    
    <h:inputText id="first" value=#{myBean.first}></h:inputText>
    <h:inputText id="second" value=#{myBean.second}></h:inputText>
    <h:commandButton id="submit" action="#{myBean.submit}" value="Submit"/>
</h:form>

Javascript:

document.getElementById("myForm:first").onkeyup = function() {
    if (this.value.length > 0) {
        document.getElementById("myForm:second").disabled = true;
        document.getElementById("myForm:submit").disabled = false;
    } else {
        document.getElementById("myForm:second").disabled = false;
        document.getElementById("myForm:submit").disabled = true;
    }
};
document.getElementById("myForm:second").onkeyup = function() {
    if (this.value.length > 0) {
        document.getElementById("myForm:first").disabled = true;
        document.getElementById("myForm:submit").disabled = false;
    } else {
        document.getElementById("myForm:first").disabled = false;
        document.getElementById("myForm:submit").disabled = true;
    }
};

这里的问题是它工作正常,但我必须在外部单击一次才能启用/禁用按钮。无论如何,当将数据输入其中一个文本框时,按钮是否会自动启用而另一个文本框会被禁用?

【问题讨论】:

  • 您能发布为 myForm 生成的 HTML 吗?

标签: javascript jsf-1.2


【解决方案1】:

onload 或直接在 commandButton 标记中保持按钮为 disabled="true"。 对inpuText实现onchange或onblur方法,设置样式为commandButton disable='false'

【讨论】:

    【解决方案2】:

    使用 onchange 事件而不是 keyup

    【讨论】:

      猜你喜欢
      • 2013-09-07
      • 1970-01-01
      • 2010-11-24
      • 2014-03-26
      • 1970-01-01
      • 2013-12-02
      • 2015-06-23
      • 1970-01-01
      • 2012-07-06
      相关资源
      最近更新 更多