How to prevent the keyboard from using an a4j: commandButton in jsf?

3

I have an a4j in the header of a web: commandButton which responds to some events caused from the computer keyboard, when I press the enter in the keyboard the action of said commandButton is launched, and when I press the delete button in the keyboard travels to the previous screen wherever it is. If I remove the button, none of this happens.

How could I control it?

    
asked by AuRiCaN 02.03.2016 в 16:31
source

1 answer

3

This has nothing to do with JSF, if not with simple HTML. <a4j:commandButton ...> is rendered in a <button type="submit" ...> . By default, submit buttons have shortcuts ; in most browsers it's the same:

  • Enter: triggers the submit event
  • Delete: go back to the previous page

This problem can be solved using JavaScript:

$(document).on("keydown", function (e) {
    if (e.which === 8 && !$(e.target).is("input, textarea")) {
        e.preventDefault();
    }
});

To avoid submitting the form when an Enter is pressed, you should listen for a keyup event in the form and detect if you have pressed Enter.

$('#tuform').on('keyup', function(e) {
  var keyCode = e.keyCode || e.which;
  if (keyCode === 13) { // Se detecta Enter
    e.preventDefault();
    return false;
  }
});

PD: RichFaces has been declared discontinued by RedHat. It is recommended to use flat JSF or another extension library.

    
answered by 02.03.2016 / 16:55
source