3

I want to submit a form by pressing enter key. Wicket allows to do that till any button is attached to form and is visible. Is there any way in wicket freamwork to submit a form by simple pressing an enter key?

enter works:

Form<?> searchForm = new Form<Void>("searchForm");
add(searchForm);
searchForm.add(button);

enter doesn't work:

Form<?> searchForm = new Form<Void>("searchForm");
add(searchForm);
searchForm.add(button);
button.setVisible(false);

In both cases button is a simple AjaxButton.

speedingdeer
  • 1,236
  • 2
  • 16
  • 26

2 Answers2

7

Hide your button in the HTML with:
style="visibility:hidden;"

and then do this in the Java code:
searchForm.setDefaultButton(button);

crudh
  • 71
  • 2
1

If you don't have a button (making the button invisible means it will not be rendered in the markup) then pressing enter on a text field can not submit the form. Either make it visible (obviously) or use some javascript to perform the submit on enter.

Example with JQuery:

$(submitOnEnter);
function submitOnEnter () {
    $("body").on("keydown", "mytextfield-selector", function(event){
        if (event.keyCode == 13) {
            event.preventDefault();
            $("myform-selector").submit();
        }
    });
}
Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101