2011-08-31 27 views
0

我想在javascript鏈接輸入按鈕(在鍵盤上),以文本框和問題是有鏈接到它的兩個提交按鈕。我研究了一下,並意識到我可以使用像這樣的按鍵Enter鍵問題 - Jquery的

$('#searchbox input').bind('keypress', function(e) {}); 

如果有一個提交按鈕。

沒有任何一個有關於如何使之成爲我的代碼工作的想法?

HTML:

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

jQuery代碼:

$(document).ready(function(){ 
    $("#button1").click(function(){ 
     $.post('fet.php', { id: $('input[name="id"]', '#myForm').val() }, 
      function(json) { 
       $("input[name='age']").val(json.age); 
      }, "json"); 
    }); 

    $("#button2").click(function(){ 
     $('form#myForm').attr({action: "fetch2.php"}); 
     $('form#myForm').submit(); 
    }); 
}); 

提前感謝!

+0

好像有什麼毛病你的語法。你是否正確複製? – pimvdb

+0

@primvbd如果有些事情想編輯您的帖子後做關閉或打開括號內的jQuery plzz忽略 –

+0

我知道你的語法完全bunked或複製並粘貼錯了。這是一切嗎? – hunter

回答

0

試試這個

$('#searchbox input:text').bind('keypress', function(e) { 

    if(e.which == 13){//Enter pressed 
     //Do your stuff here 
    }  
}); 
1

你可以這樣做:

$('#searchbox input:text').keypress(function(e) { 
    e.preventDefault();//avoid submitting the form 
    if(e.which == 13){ 
     //Enter pressed 

    }  
});