简体   繁体   English

当h:inputText没有值时,禁用ah:commandButton

[英]Disable a h:commandButton when h:inputText has no value

I have two h:inputText textboxes and ah:commandButton. 我有两个h:inputText文本框和ah:commandButton。 How can i disable the button in case there is no value in either of the text box? 万一两个文本框中的值都没有,我如何禁用该按钮? Also how do i disable the other text box in case i have value in one of the text box. 另外,如果我在其中一个文本框中有值,如何禁用另一个文本框。 Please find my sample code below: 请在下面找到我的示例代码:

<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: 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;
    }
};

The problem here is it works fine, but i have to click outside once to enable/disable the button. 这里的问题是它工作正常,但是我必须单击一次外部以启用/禁用按钮。 Is there anyway the button gets enabled automatically and the other text box gets disabled the moment a data is entered into one of the text boxes? 无论如何,在将数据输入到其中一个文本框后,按钮会自动启用,而另一个文本框会被禁用吗?

onload or directly in commandButton tag keep the button as disabled="true" . onload或直接在commandButton标记中将按钮保持为disabled="true" Implement the onchange or onblur method to inpuText and set the style to the commandButton disable='false' 对inpuText实现onchange或onblur方法,并将样式设置为commandButton disable ='false'

使用onchange事件代替keyup

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

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