【问题标题】:disable button via js通过js禁用按钮
【发布时间】:2012-09-25 15:52:47
【问题描述】:

如果文本输入值长度小于3,我需要禁用按钮。

我尝试使用 js,但按钮没有被禁用,它只是改变了颜色

 <p:autoComplete id="ac"
                    value="#{bean.selectedNetCode}" 
                    completeMethod="#{bean.complete}"
                    maxlength="3"
                    size="3"
                    maxResults="10"
                    onkeyup="checkLength(this.value)">
        <p:ajax event="itemSelect" update="genButton" listener="#{bean.handleNetCodeChange}"/>
    </p:autoComplete> 



 function checkLength(value){
      if(value.length <= 2){
      document.getElementById("genButton").disabled = true;
 }

知道为什么吗?

谢谢

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

    您可以定义p:commandButton 的属性widgetVar,这样您就可以通过javascript 访问组件。

    那么,您可以使用一些方法。 From PrimeFaces 3.4 Documentation:

    • disable():禁用按钮
    • enable():启用按钮

    例子:

    <p:autoComplete onkeyup="checkLength(this.value)">/>
    <p:commandButton widgetVar="myButton" />
    
    function checkLength(value){
         if(value.length <= 2)
             myButton.disable();
    }
    

    2021 年 7 月更新

    正如 dian jin 在 cmets 中指出的,当使用 PimeFaces 4.0 到 5.0 时,您需要调用 PF('myButton').disable(); 来正确引用 widgetVar。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多