可以在不按回车键的情况下为 p:inputText 执行 valueChangeListener 吗? [英] Possible to execute `valueChangeListener` for `p:inputText` without hitting `enter` key?

查看:10
本文介绍了可以在不按回车键的情况下为 p:inputText 执行 valueChangeListener 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户更改文本并且 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 方法需要调用表单提交.这是一个服务器端事件,而不是客户端事件等等.默认情况下,只是更改和模糊输入根本不提交表单.引入一个 来实现魔法.

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 往往是您心目中工作的错误工具.改用 .

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.

这篇关于可以在不按回车键的情况下为 p:inputText 执行 valueChangeListener 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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