Submit a form when you press Enter

I have an aspx page with many buttons, and I have a search button whose event I want to trigger when the user presses enter. How can i do this?

+8
submit forms
source share
4 answers

You set the default form button:

<form id="Form1" defaultbutton="SubmitButton" runat="server"> 
+16
source share

Make it the default button of the form or panel.

Or one has the DefaultButton property, which you can set on the desired button.

+5
source share
 $(document).ready(function() { $("#yourtextbox").keypress(function(e) { setEnterValue(e); }); }); function setEnterValue( e) { var key = checkBrowser(e); if (key == 13) { //call your post method } function checkBrowser(e) { if (window.event) key = window.event.keyCode; //IE else key = e.which; //firefox return key;} 

call the above function, and this will help you find the input key and than calling the post method.

0
source share

The best way to do this is to use the enter button and click the button without a headache to write js check for each input you enter, if the user presses the enter button or not, it uses the input type submit .

But you would run into the problem that onsubmit would not work with asp.net.

I decided it very easily.

if we have this form

 <form method="post" name="setting-form" > <input type="text" id="UserName" name="UserName" value="" placeholder="user name" > <input type="password" id="Password" name="password" value="" placeholder="password" > <div id="remember" class="checkbox"> <label>remember me</label> <asp:CheckBox ID="RememberMe" runat="server" /> </div> <input type="submit" value="login" id="login-btn"/> </form> 

Now you can catch this event before submitting the form and stop it from postback and do whatever ajax you want using this jquery.

 $(document).ready(function () { $("#login-btn").click(function (event) { event.preventDefault(); alert("do what ever you want"); }); }); 
0
source share

Source: https://habr.com/ru/post/650373/


All Articles