java Enter Key Press 的行为类似于 JSF 中的 Submit

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2419572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:02:59  来源:igfitidea点击:

Enter Key Press behave like Submit in JSF

javajsfkeysubmitenter

提问by ComputerPilot

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

如何使 Enter Key Press 表现得像 JSF 中的 Submit。它适用于输入框;但不适用于 inputSecret 框

回答by BalusC

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).

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

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

如果仍然无效,只需将以下内容添加onkeypressh: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 onkeypressevents over the h:inputXXXelements expectof textareas yourself.

但是,您需要考虑 textareas。如果你有它们,那么你需要自己复制所有onkeypress事件到textareas期望h:inputXXX元素上。

回答by Santhosh

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

如果你想按 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> 

回答by Davide Consonni

tested with jsf 2. put:

用 jsf 测试 2. 放:

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

in your form

以你的形式

回答by ComputerPilot

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

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

回答by Fenton

There is an old specification that pops into my mind with this one. If you have a form that contains just oneinput 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.

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

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

MS 错误报告在这里:connect.microsoft.com:提交按钮值未随表单提交一起发布

回答by Sandip Ashok Pol

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.

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