This is a migrated thread and some comments may be shown as answers.

Submit form using Enter key

2 Answers 74 Views
NumericTextBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
qbantek
Top achievements
Rank 1
qbantek asked on 18 Apr 2012, 10:08 PM
Currently if you hit Enter while having the focus on a NumericTextbox control, the form does not submit. You can test this behavior right from telerik demos: http://demos.telerik.com/aspnet-mvc/numerictextbox/servervalidation , just hit Enter and the form won't submit at all.

I would like to simulate the default behavior as in http://jsfiddle.net/qbantek/BsDpY/ , where when a user press the Enter key while having the focus on the text box, the surrounding form gets submitted.

Thanks!
Erich

2 Answers, 1 is accepted

Sort by
0
qbantek
Top achievements
Rank 1
answered on 26 Apr 2012, 04:36 PM
My solution so far:

$(".t-input").keypress(function (e) {
    var enterKey = 13;
    if (e.which == enterKey)
        $(this).closest("form").submit();
});
0
Matt
Top achievements
Rank 1
answered on 03 Jun 2012, 11:26 PM
Thanks for your post! Helped me achieve the same result. I modified it slightly to make sure that it uses the first submit button it finds, just in case there are multiple buttons on the form.

$(".t-numerictextbox .t-input").keypress(function (e) {
    var enterKey = 13;
    if (e.which == enterKey){
        var form = $(this).closest("form");
        var button = form.find('[type="submit"]').first();
        if(button.length > 0){
            button.click();
        }
        else{
            form.submit();
        }
    }
});

Our solution processes the form differently depending on which button is clicked, so this makes sure it has the same default behaviour as a form.

Or does form.submit(); actually behave the same as clicking the first button?





Tags
NumericTextBox
Asked by
qbantek
Top achievements
Rank 1
Answers by
qbantek
Top achievements
Rank 1
Matt
Top achievements
Rank 1
Share this question
or