简体   繁体   English

JSF:如何在按下回车键时提交表单?

[英]JSF: How to submit the form when the enter key is pressed?

It would appear to be a simple requirement, but I haven't found a simple solution yet: 这似乎是一个简单的要求,但我还没有找到一个简单的解决方案:

In a JSF 1.2 / Richfaces 3.3 webapp, I have a form with input components of various types, followed by an <a4j:commandButton> and a <h:commandButton> . 在JSF 1.2 / Richfaces 3.3 webapp中,我有一个包含各种类型的输入组件的表单,后跟一个<a4j:commandButton>和一个<h:commandButton> The former resets the form, the second performs some action with the data entered. 前者重置表单,第二个对输入的数据执行一些操作。

My goal is to have this action triggered when the user presses the enter key while entering data. 我的目标是在用户输入数据时按下回车键时触发此操作。 How can I do that? 我怎样才能做到这一点?

Edit : Generally, I have more than one <h:commandButton> per <form> . 编辑 :一般来说,每个<form>我有多个<h:commandButton> <form> I'd like to designate a particular one as default action. 我想指定一个特定的默认操作。 Also, I'd like the solution to play nice with AJAX (which we use extensively). 此外,我希望解决方案与AJAX(我们广泛使用)相得益彰。

Unless you are using MSIE browser and in reality you've only one input field without a button, it should just be the default behaviour. 除非您使用的是MSIE浏览器,并且实际上您只有一个没有按钮的输入字段,它应该只是默认行为。 Otherwise probably some (autogenerated) JS code has messed it up. 否则可能一些(自动生成的)JS代码搞砸了。

If you don't have textareas in the form, an easy fix would be the following: 如果表单中没有textareas,则可以通过以下方式轻松解决:

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

Or if you have textareas and you don't want to repeat the same keypress functions over all non-textarea input elements, run the following script during window onload. 或者,如果您有textareas并且您不想在所有非textarea输入元素上重复相同的keypress功能,请在window onload期间运行以下脚本。

for (var i = 0; i < document.forms.length; i++) {
    var inputs = document.forms[i].getElementsByTagName('input');

    for (var j = 0; j < inputs.length; j++) {
        inputs[j].onkeypress = function(e) {
            e = e || window.event;
            if (event.keyCode == 13) {
                this.form.submit();
                return false;
            }
        };
    }
}

Building on BalusC's answer I came up with the following (tested on IE and FireFox): 在BalusC的回答基础上,我想出了以下内容(在IE和FireFox上测试过):

<h:form id="form" onkeypress="ifEnterClick(event, #{rich:element('searchButton')});">

where ifEnterClick is defined by: 其中ifEnterClick由以下定义:

/**
 * Handler for onkeypress that clicks {@code targetElement} if the
 * enter key is pressed.
 */
function ifEnterClick(event, targetElement) {
    event = event || window.event;
    if (event.keyCode == 13) {
        // normalize event target, so it looks the same for all browsers
        if (!event.target) {
            event.target = event.srcElement;
        }

        // don't do anything if the element handles the enter key on its own
        if (event.target.nodeName == 'A') {
            return;
        }
        if (event.target.nodeName == 'INPUT') {
            if (event.target.type == 'button' || event.target.type == 'submit') {
                if (strEndsWith(event.target.id, 'focusKeeper')) {
                    // inside some Richfaces component such as rich:listShuttle
                } else {
                    return;
                }
            }
        }
        if (event.target.nodeName =='TEXTAREA') {
            return;
        }

        // swallow event
        if (event.preventDefault) {
            // Firefox
            event.stopPropagation();
            event.preventDefault();
        } else {
            // IE
            event.cancelBubble = true;
            event.returnValue = false;
        }

        targetElement.click();
    }
}

Edit : Since selecting a value from Firefox form auto completion using the enter key fires a keydown event, but no keypress event, using onkeypress is preferable to onkeydown. 编辑 :由于使用回车键从Firefox表单自动完成中选择一个值会触发keydown事件,但是没有按键事件,使用onkeypress比onkeydown更好。

Just put this code in your JS file: 只需将此代码放入JS文件中:

$('input,textarea').live('keydown',function(e) { // submit forms on pressing enter while focus is on any input elmnt inside form
    if (e.keyCode == 13) {
        $(this).closest('form').submit();
    }
});

Use the PrimeFaces component: 使用PrimeFaces组件:

<!-- Default button when pressing enter -->
<p:defaultCommand target="submit"/>

Use this in combination with a focus component and you will rock! 将它与焦点组件结合使用,你会摇滚!

<!-- Focus on first field, or first field with error -->
<p:focus context="feesboek"/>

In retrospect, solving this problem with the means HTML provides is far less brittle and easier to maintain, as the answers to the following related question show: 回想起来,用HTML提供的方法解决这个问题远没有那么脆弱和易于维护,因为以下相关问题的答案显示:

Multiple submit buttons on HTML form – designate one button as default HTML表单上的多个提交按钮 - 默认指定一个按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM