如何使用 javascript 禁用/重新启用 JSF 组件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3802404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:17:35  来源:igfitidea点击:

How to disable/re-enable JSF component using javascript?

javajavascriptjsf

提问by mohamida

I'm developing a Java EE application (JSF2 + richfaces 3.3.3 + facelets).

我正在开发一个 Java EE 应用程序(JSF2 + richfaces 3.3.3 + facelets)。

I want to disable my h:selectOneMenu when loading my page, and when it finishes loading (using the function onload()), I want to re-enable my component. I have something like this:

我想在加载我的页面时禁用我的 h:selectOneMenu,当它完成加载时(使用函数 onload()),我想重新启用我的组件。我有这样的事情:

<ui:define name="infosHead">
     <script type="text/javascript">
        window.onload = function() {
          document.getElementById("forme1_myform:valueCh").disabled = false;
          alert("here");
        }
     </script>
</ui:define>
<ui:define name="infosBody">
   <h:form id="forme1_myform" target="_blank">
    <h:selectOneMenu id="valueCh" value="#{mybean.value}" disabled="true" >
      <f:selectItems value="#{mybean.values}" />
         <a4j:support event="onchange"
              ajaxSingle="true"
              limitToList="true"                      
              reRender="id1,id2,...."
              ignoreDupResponses="true"
              action="#{mybean.actionme}"
              oncomplete="getId();"/>
         </h:selectOneMenu>
   </h:form>
</ui:define>

this is working fine. But my bean is getting nothing (mybean.value == null).

这工作正常。但是我的 bean 一无所获(mybean.value == null)。

It's like he thinks that the component is still disabled.

就好像他认为该组件仍然处于禁用状态。

how can I make this works ?

我怎样才能做到这一点?

采纳答案by mohamida

I found this solution.

我找到了这个解决方案。

<ui:define name="infosHead">
     <script type="text/javascript">
        window.onload = function() {
          updateName(false); // sets 'disabled' from true to false
        }
     </script>
</ui:define>
<ui:define name="infosBody">
   <h:form id="forme1_myform" target="_blank">
    <h:selectOneMenu id="valueCh" value="#{mybean.value}" disabled="#{mybean.render}" >
      <f:selectItems value="#{mybean.values}" />
         <a4j:support event="onchange"
              ajaxSingle="true"
              limitToList="true"                      
              reRender="id1,id2,...."
              ignoreDupResponses="true"
              action="#{mybean.actionme}"
              oncomplete="getId();"/>
         </h:selectOneMenu>
   </h:form>
    <a4j:form>
        <!-- this is where it's going to reRender my component -->
        <a4j:jsFunction name="updateName" reRender="valueCh">
            <a4j:actionparam name="param1" assignTo="#{mybean.render}"  />
        </a4j:jsFunction>
    </a4j:form>
</ui:define>

and this is the content of mybean:

这是 mybean 的内容:

public class MyBean implements Serializable {

    private List<SelectItem> values;
    private String value;
    private Boolean render = true; // the page loads with element disabled

    public void actionme(){...}
    //getters & setters
}

回答by Giorgos Dimtsas

The problem is that you are enabling your component only on the client side. On the server side it will always be disabled="true". To make this work you must :

问题是您仅在客户端启用您的组件。在服务器端,它将始终是disabled="true". 要完成这项工作,您必须:

a. Assign the disabled property of your component to a managed bean property that will be initially 'true'

一个。将组件的 disabled 属性分配给最初为“true”的托管 bean 属性

disabled="#{myController.valueChDisableStatus}"

disabled="#{myController.valueChDisableStatus}"

b. Inside your h:form insert window.onload = callScript

湾 在你的 h:form 中插入 window.onload = callScript

c. Finally, in the myController#someActionmethod set the valueChDisableStatusproperty to false

C。最后,在myController#someAction方法中将valueChDisableStatus属性设置为false

I cant check the solution right now, but I believe it will work fine.

我现在无法检查解决方案,但我相信它会正常工作。

回答by Joseph Cozad

Neither of these solutions worked for me.

这些解决方案都不适合我。

While setting the element's disabled attribute from a managed bean value worked fine, and changing it's value via javascript also worked fine, once the form is submitted the "someaction" method is still not reached in order to "set the valueChDisableStatus property to false". In addition I found that even though my managed bean had an isDisabled() and a setDisabled(boolean value), the setDisabled() method was never called after submitting the form (of course), though the isDisabled() was. So I was unable to find a way using these solutions to change the managed beans "disabledElement" value to false.

虽然从托管 bean 值设置元素的禁用属性工作正常,并且通过 javascript 更改它的值也工作正常,但一旦提交表单,仍然没有达到“someaction”方法以“将 valueChDisableStatus 属性设置为 false”。此外,我发现即使我的托管 bean 有一个 isDisabled() 和一个 setDisabled(boolean value),但在提交表单后从未调用 setDisabled() 方法(当然),尽管 isDisabled() 是。因此,我无法找到使用这些解决方案将托管 bean 的“disabledElement”值更改为 false 的方法。

I did find a solution that did work for me though at this link: http://blog.michaelscepaniak.com/jsf-you-cant-submit-a-disabled-form-element

我确实在此链接中找到了对我有用的解决方案:http: //blog.michaelscepaniak.com/jsf-you-cant-submit-a-disabled-form-element

Basically it suggests that by default all the elements should be "enabled" from the JSF perspective, and that all enabling and disabling should be done via Javascript. It does point out the the client and server states are out of sync temporarily in this solution... but it works very well for my scenario regardless of the temporary mismatch.

基本上它建议从 JSF 的角度默认所有元素都应该“启用”,并且所有启用和禁用都应该通过 Javascript 完成。它确实指出客户端和服务器状态在此解决方案中暂时不同步......但无论临时不匹配如何,它都适用于我的场景。