2017-02-13 228 views

回答

1

試試這個:

的Jquery:

$('.search_text').keypress(function(e){ 
    if(e.which == 13){//Enter key pressed 
     // do something here 
    } 
}); 

JS:

document.getElementById("id_of_textbox") 
    .addEventListener("keyup", function(event) { 
    event.preventDefault(); 
    if (event.keyCode == 13) { 
     // Do something 
    } 
}); 

Working Fiddle

+0

不pandeyz –

+0

工作@mayang雅THX @mayank pandeyz –

0

一種可能的方式做,這將與keydown事件。

$(document).on('keydown', '.search_text', function (e) { 
 
    var keyCode = e.keyCode || e.which; 
 
    if(keyCode === 13) { 
 
    // prevent default action 
 
    e.preventDefault(); 
 

 
    // get input value 
 
    console.log($(this).val()); 
 
    } 
 
});
Type something and press enter<br> 
 
<input type="text" class="search_text"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>