根据组合值更改输入 [英] Change input based on combo value

查看:46
本文介绍了根据组合值更改输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题在SO的其他地方仍然存在,但是要么解决方案过时(并且JSF似乎已经改进了很多),要么我无法使该解决方案正常工作.

I know this question exists somewhere else in SO but either the solutions are old (and JSF seems to have improved a lot) or I cannot make the solution work.

听起来很简单,我想根据组合框的值替换输入元素的文本.我想使用Ajax,并且即使组合中只有一个元素,也希望它能工作(默认情况下,组合的选择为空并不重要).

As simple as it sounds, I would like to replace the text of an input element based on the value of a combo box. I would like to use Ajax, and would like this to work even if there is only one element in the combo (it doesn't matter if by default the selection of the combo is empty).

<h:selectOneMenu id="fnamecombo" valueChangeListener="#{namesController.setForename(fnamecombo)}">
    <c:forEach items="#{namesController.myForenames}" var="myforename">
        <f:selectItem itemValue="#{myforename}" itemLabel="#{myforename}" />
    </c:forEach>
    <f:ajax render="fnameinput"  />
</h:selectOneMenu>

<h:inputText value="#{namesController.forename}" id="fnameinput" />

这不起作用.因此,首先,我不知道如何调用setForename方法.如果我使用valueChangeListener="#{namesController.setForename('xxxxx')}",它可以工作,但是只有第一次并且组合中有多个元素时,否则该事件似乎不会被触发.

This doesn't work. So first of all, I have no idea how to call the setForename method. If I use valueChangeListener="#{namesController.setForename('xxxxx')}" it works, but only the 1st time and iff there are more than one element in the combo, since otherwise the event does not seem to be fired.

什么是简单的解决方法?

What is the easy fix?

编辑

好的,所以我已经取得了进步.这比我预期的要容易:

Ok, so I have made progress. It was easier than I expected:

<h:selectOneMenu id="fnamecombo" value="#{namesController.forename}">
    <c:forEach items="#{namesController.myForenames}" var="myforename">
        <f:selectItem itemValue="#{myforename}" itemLabel="#{myforename}" />
    </c:forEach>
    <f:ajax render="fnameinput"  />
</h:selectOneMenu>

<h:inputText value="#{namesController.forename}" id="fnameinput" />

这似乎可以在我手动创建的selectItem上使用,但不适用于使用foreach循环进行打印的版本.这就是渲染的代码,我从循环中获得了"john",然后手动创建了"example":

This seems to work on a selectItem that I create by hand, but not on the one that is printing with the foreach loop. So this is the rendered code, where I obtained 'john' from the loop and I manually created 'example':

<select id="myForm:fnamecombo" name="myForm:fnamecombo" size="1" onchange="mojarra.ab(this,event,'valueChange',0,'myForm:fnameinput')">
    <option value="example">example</option>
    <option value="john">john</option>
</select>

它适用于示例",但不适用于约翰".

It works with 'example' but not with 'john'.

推荐答案

最后我得到了答案.

<h:selectOneMenu id="fnamecombo" value="#{namesController.forename}">
    <f:selectItems value="#{namesController.myForenames}" />
    <f:ajax render="fnameinput"  />
</h:selectOneMenu>

<h:inputText value="#{namesController.forename}" id="fnameinput" />

不需要Alexandre Lavoie提到的forEach.

No need for forEach as mentioned by Alexandre Lavoie.

Luiggi Mendoza的答案给了我寻找答案的提示.我的输入没有被f:selectItems中的值更新的原因,并且是我手动引入的,这是受管bean的范围.我意识到在任何情况下input实际上都在更新,但是当来自f:selectItems时,input被更新为null.为什么?因为namesController的范围是@RequestScoped而不是@ViewScoped.更改此设置即可解决问题.

This answer by Luiggi Mendoza gave me the hint to find it out. The reason my input was not updated by the values in the f:selectItems and it was in the ones that I introduced manually is the scope of the managed bean. I realised that the input was actually being updated in any case, but when coming from the f:selectItems the input was updated with null. Why? Because the scope of namesController was @RequestScoped and not @ViewScoped. Changing this solves the problem.

这篇关于根据组合值更改输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆