Search code examples
javajava-8wicketwicket-6

Form and Enter key submission in wicket7


I have form with Textbox and submit Ajaxbutton, when i insert some value in textbox then click on submit button, will perform some business logic written in OnSubmit of Ajaxbutton, its fine for us But when i insert some value in textbox then press enter key so i want to perform same business logic of OnSubmit of Ajaxbutton but it doesn't call, Please suggest @martin-g . I want to prefer wicket code instead of JavaScript.

Below is my code in my application.

<Form wicket:id="FilterForm">
    <input type="textbox" wicket:id="input"></input>
    <input type="submit" wicket:id="submit"/>
</Form>

for java code:

final Form filterForm = new Form("FilterForm"){}
add(filterForm);
filterForm.setMarkupId("filterForm");
filterForm.setOutputMarkupId(true);

TextField<String> docIdInput = new TextField("input", new PropertyModel(this.searchObj, "searchString"));
    filterForm.add(docIdInput);
    docIdInput.setOutputMarkupId(true);

AjaxButton docIdInputSubmitButton = new AjaxButton("submit") {

        private static final long serialVersionUID = 1L;
        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
        //do buisness logic
        }

filterForm.add(docIdInputSubmitButton);

Solution

  • You can make your button the default button of the form:

    filterForm.setDefaultButton(docIdInputSubmitButton);
    

    Wicket will generate some JavaScript for you that triggers your submit button on enter.