PrimeFaces disable submit on pressing enter key

前端 未结 6 1119
太阳男子
太阳男子 2021-01-19 07:51

PrimeFaces disable submit on pressing enter key.

I’m, running PrimeFaces 5.1 running on WildFly 8.2 Final.

I have dialog, with two inputNumbers and two butt

6条回答
  •  孤独总比滥情好
    2021-01-19 07:58

    As the answer referenced by Nimnio says, this is specific to HTML and browsers.

    I consider this behavior to be inappropriate when using PrimeFaces. I prefer to disable it globally, for all forms like this:

    $('form').off('keypress.disableAutoSubmitOnEnter').on('keypress.disableAutoSubmitOnEnter', function(event) {
        if (event.which === $.ui.keyCode.ENTER && $(event.target).is(':input:not(textarea,:button,:submit,:reset)')) {
            event.preventDefault();
        }
    });
    

    The target check allows the other default behaviors to work, like adding a line break in a textarea by pressing Enter.

    To take into account new ajaxically added forms you'll need to call the above script after every AJAX request. There are multiple ways to do that, such as a

提交回复
热议问题