按Enter键时提交表单

时间:2022-01-16 09:05:41

I have an aspx page with many buttons and i have a search button whose event i want to be triggered when user press enter. How can i do this?

我有一个带有许多按钮的aspx页面,我有一个搜索按钮,当用户按下回车时我想要触发该事件。我怎样才能做到这一点?

4 个解决方案

#1


16  

You set the forms default button:

您设置表单默认按钮:

<form id="Form1"
    defaultbutton="SubmitButton"
    runat="server">

#2


6  

Make it the default button of the form or panel.

将其设置为表单或面板的默认按钮。

Either one has a DefaultButton property that you can set to the wanted button.

任何一个都有一个DefaultButton属性,您可以将其设置为所需按钮。

#3


0  

$(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 it will help you in detecting the enter key and than call your post method.

调用上面的函数,它将帮助您检测回车键,而不是调用您的post方法。

#4


0  

The best way to make from make actions using enter button and on click of the button without the headache to write js check for each input you have if the user press enter or not is using submit input type.

使用输入按钮和单击按钮进行操作的最佳方法,如果用户按下输入或不使用提交输入类型,则无需编写js检查每个输入。

But you would face the problem that onsubmit wouldn't work with asp.net.

但是你会遇到onsubmit无法与asp.net一起工作的问题。

I solved it with very easy way.

我用非常简单的方法解决了它。

if we have such a 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>

You can now catch get that event before the form postback and stop it from postback and do all the ajax you want using this jquery.

您现在可以在表单回发之前捕获该事件并从回发中停止它并使用此jquery执行您想要的所有ajax。

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

#1


16  

You set the forms default button:

您设置表单默认按钮:

<form id="Form1"
    defaultbutton="SubmitButton"
    runat="server">

#2


6  

Make it the default button of the form or panel.

将其设置为表单或面板的默认按钮。

Either one has a DefaultButton property that you can set to the wanted button.

任何一个都有一个DefaultButton属性,您可以将其设置为所需按钮。

#3


0  

$(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 it will help you in detecting the enter key and than call your post method.

调用上面的函数,它将帮助您检测回车键,而不是调用您的post方法。

#4


0  

The best way to make from make actions using enter button and on click of the button without the headache to write js check for each input you have if the user press enter or not is using submit input type.

使用输入按钮和单击按钮进行操作的最佳方法,如果用户按下输入或不使用提交输入类型,则无需编写js检查每个输入。

But you would face the problem that onsubmit wouldn't work with asp.net.

但是你会遇到onsubmit无法与asp.net一起工作的问题。

I solved it with very easy way.

我用非常简单的方法解决了它。

if we have such a 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>

You can now catch get that event before the form postback and stop it from postback and do all the ajax you want using this jquery.

您现在可以在表单回发之前捕获该事件并从回发中停止它并使用此jquery执行您想要的所有ajax。

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