簡體   English   中英

輸入按鈕問題-jQuery

[英]Enter Button Issue — Jquery

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

$('#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();
    });
});

提前致謝!

您可以這樣做:

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

   }    
});

嘗試這個

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM