java Seam/JSF 表单提交触发按钮 onclick 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/117189/
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
Seam/JSF form submit firing button onclick event
提问by sblundy
I have a search form with a query builder. The builder is activated by a button. Something like this
我有一个带有查询构建器的搜索表单。构建器由一个按钮激活。像这样的东西
<h:form id="search_form">
<h:outputLabel for="expression" value="Expression"/>
<h:inputText id="expression" required="true" value="#{searcher.expression}"/>
<button onclick="openBuilder(); return false;">Open Builder</button>
<h:commandButton value="Search" action="#{searcher.search}"/>
</h:form>
The result is HTML that has both a <button/>and an <input type="submit"/>in the form. If the user enters a string into the expression field and hits the enter key rather than clicking the submit button, the query builder is displayed when the expected behavior is that the search be submitted. What gives?
结果是表单中同时包含 a<button/>和 an 的HTML <input type="submit"/>。如果用户在表达式字段中输入一个字符串并按回车键而不是单击提交按钮,则在预期行为是提交搜索时会显示查询构建器。是什么赋予了?
采纳答案by noah
A button in an HTML form is assumed to be used to submit the form. Change button to input type="button" and that should fix it.
假定 HTML 表单中的按钮用于提交表单。将按钮更改为 input type="button" 应该可以修复它。
Alternatively, add type="button" to the button element.
或者,将 type="button" 添加到按钮元素。
回答by stefano m
as first, give an ID to Search button. Then,on textbox, you could intercept client event onkeydown, with a (javascript) function like this:
首先,给“搜索”按钮一个 ID。然后,在文本框上,您可以使用如下(javascript)函数拦截客户端事件onkeydown:
function KeyDownHandler(event)
{
// process only the Enter key
if (event.keyCode == 13)
{
// cancel the default submit
event.returnValue=false;
event.cancel = true;
// submit the form by programmatically clicking the specified button
document.getElementById('searchButtonId').click();
}
}
I hoper i help you.
我希望我能帮助你。
回答by user13229
if there is a single input fieldwithin the form, many browsers submit the forms automatically when the enter key is hit.
如果表单中只有一个输入字段,当按下回车键时,许多浏览器会自动提交表单。
Try
尝试

