提交表单数据上都点击进入,打提交按钮 [英] Submit form data both on clicking enter and hitting the submit button

查看:137
本文介绍了提交表单数据上都点击进入,打提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我有一个表格:

<form class="form-horizontal" id="login-form">
<input name="email" id="email" type="email" size="30" class="span4" placeholder="Email" style="margin-top:20px; margin-left:20px; width: 270px;"/>
<br>
<input name="passwd" id="passwd" type="password" class="span4" placeholder="Password" style="margin-top:10px; margin-left:20px; width: 270px;"/>
<br><br>
<input type="button" id="login" name="login" value="Go" class="btn btn-custom" style="width:30%; margin-left:30px; margin-top:20px"/>
 <br>
 <br>
</form>  

我让使用JavaScript提交关于单击该按钮的数据在这里,一个Ajax调用:

Here a ajax call I make using JavaScript to submit the data on clicking the button:

$('#login').click(function(){
    $.ajax({
        url:"script.php",
        type:'POST',
        dataType:"json",
        data: $("#login-form").serialize()
    }).done(function(data){
       //do domething
    });
});

如何通过pressing回车键并点击提交按钮提交数据两者兼而有之?

How can I submit the data both by pressing the enter key and clicking on the submit button?

推荐答案

我已经更新code。创建一个函数,并调用它在两个文本框键preSS事件,并确定提交按钮,然后调用功能和按钮的单击事件。

I have updated code. Create a function and call it on both textbox keypress event and identify submit button then call function and on click event of button.

function submitLogin() {
$.ajax({
    url:"script.php",
    type:'POST',
    dataType:"json",
    data: $("#login-form").serialize()
}).done(function(data){
   //do domething
});
}

$('#email').keypress(function(e) {
if (e.which == '13') {
         submitLogin();
   }
});
$('#passwd').keypress(function(e) {
if (e.which == '13') {
         submitLogin();
   }
});
$('#login').click(function(){
    submitLogin();
});

这篇关于提交表单数据上都点击进入,打提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆