2011-08-30 39 views
1

:我有JS製作回車鍵(鍵盤)提交我的形式看起來像這樣的信息--Jquery

<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"/> 

,看起來像這樣

$(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以使輸入按鈕(在鍵盤上)也提交表單的任何想法?

注意:有兩個提交按鈕都相互關聯。

+0

如果用戶點擊輸入確實,哪個提交被觸發?這可能有所幫助:http://stackoverflow.com/questions/302122/jquery-event-keypress-which-key-was-pressed – dmcnelis

+0

刷新......... –

+0

所以當你說相通,你的意思是他們兩者都觸發相同的動作呢? – dmcnelis

回答

1

你可以給你的輸入元素的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() 
    } 
}); 
0

此代碼適用於 我。

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