Enter 键按下的行为类似于 JSF 中的提交

发布于 2024-08-24 05:49:51 字数 56 浏览 1 评论 0原文

如何使 Enter 键按下的行为类似于 JSF 中的提交。它与输入框一起使用;但不适用于输入秘密框

How to make Enter Key Press behave like Submit in JSF. It works with InputBoxes; but not with inputSecret boxes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

泪眸﹌ 2024-08-31 05:49:51

我以前没见过这个问题。此行为不太可能是特定于浏览器的。尝试在不同类型的浏览器中排除其中一种或另一种(IE6/7/8、FF、Safari、Chrome 等)。更有可能的是,这是由(糟糕的)Javascript 键事件侦听器引起的,它错误地抑制了 Enter 键(例如检查输入字符的 JS 密码验证器)。

如果仍然无效,只需将以下 onkeypress 添加到 h:form 即可:

<h:form onkeypress="if (event.keyCode == 13) this.submit();">

但是,您需要考虑文本区域。如果您有它们,那么您需要自己复制文本区域的 h:inputXXX 元素上的所有 onkeypress 事件。

I haven't seen this issue before. The chance is little that this behaviour is browser specific. Try in different kinds of browsers to exclude the one and other (IE6/7/8, FF, Safari, Chrome, etc). The chance is bigger that this is caused by a (poor) Javascript key event listener which incorrectly suppresses the enter key (for example a JS password validator which checks the entered characters).

If still in vain, just add the following onkeypress to the h:form:

<h:form onkeypress="if (event.keyCode == 13) this.submit();">

You need to take textareas into account however. If you have them, then you need to copy all onkeypress events over the h:inputXXX elements expect of textareas yourself.

山人契 2024-08-31 05:49:51

如果你想按 ENTER 键而不是以任何形式提交,我们在这里要做的就是在 af:form 标签中添加 defaultcommand 属性,并给出提交按钮的 id 作为值。示例代码片段是

<af:form id="f1" defaultCommand="cb1">
<af:outputText value="User Name" id="usename"/>
          <af:inputText value="#{BackingBean.userName}" id="uname" />
          <af:outputText value="Password" id="pword"/>
          <af:inputText value="#{BackingBean.password}" id="paword" secret="true"/>
          <af:commandButton text="Submit" action="#{BackingBean.method}" id="cb1" />
</af:form> 

If you want to press ENTER key instead of submit in any form, what we have to do here is add defaultcommand attribute in af:form tag and give id of the submit button as value. Sample code snippet for this is

<af:form id="f1" defaultCommand="cb1">
<af:outputText value="User Name" id="usename"/>
          <af:inputText value="#{BackingBean.userName}" id="uname" />
          <af:outputText value="Password" id="pword"/>
          <af:inputText value="#{BackingBean.password}" id="paword" secret="true"/>
          <af:commandButton text="Submit" action="#{BackingBean.method}" id="cb1" />
</af:form> 
城歌 2024-08-31 05:49:51

我通过放置一个额外的 inputBox 并使用 javascript 隐藏它来使其工作。如果您有任何其他建议,请告诉我
感谢 baluC 为我指明了正确的方向
杰瑞

I made it work by placing an additional inputBox and hiding it using javascript. Let me know if you have any other suggestions
Thanks baluC for pointing me in the right direction
Jerry

天涯离梦残月幽梦 2024-08-31 05:49:51

使用 jsf 2 进行测试。 put:

<h:commandButton id="hidden" style="visibility: hidden;" action="#{mybean.myaction()}" />

在您的表单中

tested with jsf 2. put:

<h:commandButton id="hidden" style="visibility: hidden;" action="#{mybean.myaction()}" />

in your form

燕归巢 2024-08-31 05:49:51

从 ComputerPilot 的回答中,我了解到这是 IE 中的错误,如果只有一个输入元素,则不会使父表单提交。为了解决这个问题,我又添加了一个带有属性 style="display:none" 的输入框,它工作得很好。

From ComputerPilot's answer I came to know that it's bug in IE which wouldn't make the parent form to submit if there's only one input element. To overcome this problem I added one more input Box with attribute style="display:none" and it worked fine.

嘴硬脾气大 2024-08-31 05:49:51

我的脑海中突然浮现出一个旧的规范。如果您的表单仅包含一个输入字段,则通过 enter 键提交的行为在某些版本的 Internet Explorer 中不起作用。最简单的解决方法是确保您有多个输入字段。

此问题的其他原因包括...

  • 没有提交类型的输入
  • 具有提交类型的输入,但它在页面上不可见(隐藏或位于页面外)

此行为非常特定于此浏览器。

此处的 MS 错误报告:connect.microsoft.com:提交按钮值未随表单提交一起发布

There is an old specification that pops into my mind with this one. If you have a form that contains just one input field, the behaviour of submitting on the enter-key doesn't work in some versions of Internet Explorer. The easiest fix is to make sure you have more than one input field.

Other reasons for this problem include...

  • Not having an input of type submit
  • Having an input of type submit, but it isn't visible on the page (hidden or positioned off-page)

This behaviour is very specific to this browser.

MS Bug Report Here: connect.microsoft.com: submit button value not posted with form submission

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文