javascript - Handle user hitting 'Enter' key in a ASP.NET MVC web site -
I am working on an ASP.NET MVC web site that has multiple submit buttons. I.e.
& lt; Input type = "submit" name = "submit button" value = "reset" /> & Lt; Input type = "submit" name = "submitButton" value = "OK" /> & Lt; Input type = "submit" name = "submitButton" value = "back" />
I have to allow users to quickly submit a form by pressing the 'Enter' key. The HTML standard specifies that if a user presses the 'Enter' key then the first submit button will be assumed. However, I need to make the default button (i.e. "OK") button, and for reasons I do not want to talk about it, changing the button order is not an option
I googled around And I found about the page. ASP.NET in Form.DefaultButton but it does not work with ASP.NET MVC.
I also tried the following Javascript solution, while it works in Chrome but does not work in IE6
$ ('body') Keypress (function (e) {if (e.which === 13) {$ ("input [value = 'OK']"). Trigger ('click');}});
I can think of some of the most extreme solutions such as attaching the above mentioned tasks to each single control in the form. However, I do not think this is a very clear solution, so I am thinking that anyone has got a better solution?
First of all, this is incorrect:
& lt; Input type = "submit" name = "submitButton" value = "reset" /> & Lt; Input type = "submit" name = "submitButton" value = "OK" /> & Lt; Input type = "submit" name = "submitButton" value = "back" />
Submit all those three buttons Reset is an input type = "reset", solve it First of all, I have successfully implemented something like this, and it works on IE6 is. Try it:
function keypressHandler (e) {if (e.which == 13) {e.preventDefault (); // stops the default task: submitting form $ (this). Blur (); $ ('# SubmitButton'). Focus () Click (); // Submit one of your IDs}} $ ('# myForm'). Keypress (keypress handler);
Focus () creates part when the button is pressed when the user presses enter enough Nifty.
Comments
Post a Comment