输入键(键盘上)提交信息——Jquery

Making Enter key (on keyboard) to submit the information --Jquery

本文关键字:提交 信息 Jquery 键盘 输入      更新时间:2023-09-26

我的表单是这样的:

<body>
<form id="myForm" method="post">
<label>id:</label> 
<input type="text" name="id" id="id" size="50"/>
<div id="hidden" style="display: none;">
<label>Name:</label>
<input type="text" name="name" size="50"/><br/>
<input type="button" id="button2" value="Update" size="25" /> <br/>
</div> 
<input type="button" id="button1" value ="Get Info" onclick="document.getElementById('hidden').style.display = ''; this.style.display = 'none'" size="25"/>  

我有这样的JS

$(document).ready(function(){  
$("#button1").click(function(){
    $.post(
        'xxx.php',
        { id: $('input[name="id"]', '#myForm').val() },
        function(json) { 
            if (json.abc === 'no'){
             alert('does not exist');   
            }
            else{
            $("input[name='name']").val(json.name);
            }},
        "json"
    );
});
$("#button2").click(function(){
    $('form#myForm').attr({action: "xxx1.php"});
    $('form#myForm').submit();
});
});

问题是用户只能通过点击提交按钮来提交这个表单。关于如何调整js以便输入按钮(键盘上)也提交表单的任何想法?

:有两个提交按钮都是相互链接的

您可以为输入元素指定一个id,以便于检索:

<input id="txtName" type="text" name="name" size="50"/><br/>

那么你可以将你的函数绑定到按键事件:

$('#txtName').keypress(function (e) {
  if (e.which == 13) {
    $("#button1").click()
  }
});

或者,对于一般情况,您可能只想将函数绑定到表单的每个文本框:

$('#myForm input:text').keypress(function (e) {
  if (e.which == 13) {
    $("#button1").click()
  }
});

这段代码适合我。

$('#yourform').bind('submit', function() {