2014-11-25 186 views
-1

任何人都可以教我如何在按下鍵盤「Enter」時使輸入按鈕點亮?輸入鍵盤輸入按鈕

我從網上學習的東西就是這樣的,但是當我按下鍵盤輸入時它不起作用。任何人都可以告訴我如何?

<input type="text" id="info" name="info" /> 
<input type="button" id="bn" name="bn" value="click" onclick="read()" /> 

<script> 

$(document).ready(function(){ 
    $('#info').keypress(function(e){ 
     if(e.keyCode==13) 
     $('#bn').click(); 
    }); 
}); 

function read() 
{ 
// do smth 
} 
</script> 
+3

你的代碼應該可以工作。 – Satpal 2014-11-25 07:09:06

+0

你的代碼很好。看到它在這裏工作:http://jsfiddle.net/1wt90mty/ – geevee 2014-11-25 07:09:37

+0

但當我點擊輸入它什麼都沒有發生...... – 2014-11-25 07:13:25

回答

0

我加入小提琴:http://jsfiddle.net/nzu27rw4/1/ 確保你已經對這個腳本的頂部添加jQuery的參考

$(document).ready(function(){ 
 
    $('#info').keypress(function(e){ 
 
     if(e.keyCode==13) 
 
     $('#bn').click(); 
 
    }); 
 
}); 
 

 
function read() 
 
{ 
 
alert('read') 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<input type="text" id="info" name="info" /> 
 
<input type="button" id="bn" name="bn" value="click" onclick="read()" />

1

試試這個代碼:

<input type="text" id="info" name="info" /> 
    <input type="button" id="bn" name="bn" value="click" onclick="read()" /> 

    <script> 

    $(document).ready(function(){ 
    $('#info').keyup(function(e){ 
     if(e.keyCode==13) 
     $('#bn').trigger("click"); 
    }); 
}); 

function read() 
{ 
alert('clicked') 
} 
    </script>