问题描述
当用户更改文本并且inputText失去焦点(onchange)时,我想对p:inputText
执行valueChangeListener
.这可能吗?目前,它仅在按回车键后执行.
I'd like to execute valueChangeListener
for p:inputText
when user changes text and inputText looses focus (onchange). Is this possible? For now it only executed after I press return.
推荐答案
valueChangeListener
方法需要调用表单提交.这是服务器端事件,而不是客户端事件等.默认情况下,仅更改和模糊输入不会完全提交表单.引入<p:ajax>
做魔术.
The valueChangeListener
method requires a form submit to be invoked. This is a server side event, not a client side event or so. Just changing and blurring the input does by default not submit the form at all. Bring in a <p:ajax>
to do the magic.
<p:inputText value="#{bean.inputValue}" valueChangeListener="#{bean.inputChanged}">
<p:ajax />
</p:inputText>
但是,尽管,您并没有告诉您要满足的具体功能要求认为这是正确的解决方案,我只想提及valueChangeListener
通常不是您所想到的工作的错误工具.请使用<p:ajax listener>
.
However, although you didn't tell anything about the concrete functional requirement for which you thought that this is the right solution, I just wanted to mention that the valueChangeListener
is more than often the wrong tool for the job you had in mind. Use <p:ajax listener>
instead.
<p:inputText value="#{bean.inputValue}">
<p:ajax listener="#{bean.inputChanged}" />
</p:inputText>
请注意,这将使EL 2.2可以传递方法参数,这将立即回答.
Note that this would then make it possible to pass method arguments by EL 2.2, which would immediately then answer the possible underlying functional requirement of your other — actually pretty poor — question.
<p:inputText value="#{bean.inputValue}">
<p:ajax listener="#{bean.inputChanged('arg1', 'arg2')}" />
</p:inputText>
还请注意,如果您实际上对输入的值感兴趣,那么这可能根本不是解决方案.您可以直接访问inputValue
属性.
Also note that this might not be the solution at all if you're actually interested on the entered value; you could just access the inputValue
property directly.
这篇关于是否可以在不按Enter键的情况下为p:inputText执行valueChangeListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!