简体   繁体   English

按下回车键时会调用a4j:commandButton

[英]a4j:commandButton gets invoked when hitting enter

I have a span of two components, a search field and a command button. 我有两个部分,一个搜索字段和一个命令按钮。 The command button is for a separator task. 命令按钮用于分隔符任务。 So when the input field gets focused and the user hits enter, it should redirect to a search result page and the command button shouldn't be invoked. 因此,当输入字段成为焦点并且用户按下Enter键时,它应该重定向到搜索结果页面,并且不应调用命令按钮。

<s:span>
    <h:inputText value="#{search.input}" onkeypress="performSearch(event)"/>
    <a4j:commandButton image="icon.jpg" action="#{some action}" ajaxSingle="true">
    </a4j:commandButton>
</s:span>

performSearch is a javascript function which invoke the actual search on Enter. performSearch是一个JavaScript函数,可在Enter上调用实际搜索。

Now, the action of the commandButton gets invoked when hitting enter on the input field. 现在,在输入字段上按Enter时,将调用commandButton的动作。 Any idea to fix it? 有解决的办法吗?

The onkeypress should return false when the enter key is been pressed. 当按下回车键时, onkeypress应该返回false This will block the default action of the enter key. 这将阻止Enter键的默认操作。

Thus so, 因此,

onkeypress="return performSearch(event)"

with something like 用类似的东西

function performSearch(event) {
    // ...

    if (notEnterPressed) {
        return true;
    }

    // Perform search.
    // ...

    return false;
}

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

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