【问题标题】:focus between an inputText and commandButton when multiple inputText and commandButton多个 inputText 和 commandButton 时的 inputText 和 commandButton 之间的焦点
【发布时间】:2015-11-24 10:56:24
【问题描述】:

我正在使用 Richfaces 和 jsf。

我有一个看起来像这样的表格:

<h:form>

    <h:inputText id="id1" value="#{mybean.1}" />
    <h:commandButton id="button1" action="#{otherBean.goToPage1}">
        <f:ajax execute="id1" render="id1" />
    </h:commandButton>

    <h:inputText id="id2" value="#{mybean.2}" />
    <h:commandButton id="button2" action="#{otherBean.goToPage2}">
        <f:ajax execute="id2" render="id2" />
    </h:commandButton>        

    <h:inputText id="id3" value="#{mybean.3}" />
    <h:commandButton id="button3" action="#{otherBean.goToPage3}">
        <f:ajax execute="id3" render="id3" />
    </h:commandButton>

</h:form>

我的问题是,当我在输入“id3”上写一些东西并按 ENTER 键时,“id1”的命令按钮正在读取。 或者我想要的是,当我在输入中按 Enter 时,好的 commandButton 正在读取(button1 的 input1,button2 的 input2,...)

对于我的项目,我只需要使用一种形式。

有人有想法吗?

【问题讨论】:

  • 您忘记关闭"id2" commandButton 组件。
  • 谢谢,但问题仍然存在
  • 解决你的问题...首先确保没有默认的命令按钮(如何在stackoverflow中进行搜索)。然后在 Stackoverflow 中寻找一个 Q/A 来检测输入中的 Enter-press 并通过 javascript 使“单击”右键。
  • 如果我理解正确的话,表单上的第一个按钮是默认按钮。我尝试包含一些 javascript,但即使经过研究,我也找不到解决我问题的解决方案
  • “如果我理解正确,表单上的第一个按钮是默认按钮。”:正确

标签: jsf richfaces


【解决方案1】:

我找到了丢失的东西:

<h:inputText id="id1" value="#{mybean.1}" 
             onkeyup="if (event.keyCode == 13) { #{rich:element('button1')}.click();}"
             onkeydown="if (event.keyCode == 13) { #{rich:element('button1')}.click();}"/>
<h:commandButton id="button1" action="#{otherBean.goToPage1}">
    <f:ajax execute="id1" render="id1" />
</h:commandButton>

必须在每个 inputText 和 #{rich:element('button1')} 替换 document.getElementById('button1')

对于 Internet Explorer,我添加:onblur="#{rich:element('button1')}.click();"

<h:inputText id="id1" value="#{mybean.1}" 
             onkeyup="if (event.keyCode == 13) { #{rich:element('button1')}.click();}"
             onkeydown="if (event.keyCode == 13) { #{rich:element('button1')}.click();}"
             onblur="#{rich:element('button1')}.click();" />

它适用于 IE8 和 IE11。

【讨论】:

  • 为什么要同时使用 keyup 和 keydown?
  • keyup 是有效的,但是没有 keydown,我的真实代码有一些小错误......我不知道为什么
  • 我刚刚意识到该代码在 Firefox 或 Chrome 中运行良好,但在 IE 上无法运行
  • 我记得关键代码甚至事件都存在问题。但是您必须说明在其他浏览器中究竟发生了什么...添加 e.d. console.log 语句
  • 我在 IE 控制台中什么都没有,但它仍然不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
  • 2013-02-12
  • 1970-01-01
  • 2012-02-11
相关资源
最近更新 更多