简体   繁体   中英

enter key press event is not working

This is Not working

HTML

<input type="text" class="form-control" id="newtopic" placeholder="Add Topic">  

Script

$(document).bind('keypress',function(){
$("#newtopic").keydown(function(e){ 
    if(e.keyCode==13){
        alert("Hello World");
    }
}); });

Try this

$(document).ready(function(){
$("#newtopic").keypress(function(e){ 
    if(e.keyCode==13){
        alert("Hello World");
    }
 }); 
});

DEMO

Try with this

$(document).keypress(function(e) {
if(e.keyCode == 13) {
    alert('You pressed enter!');
}
});

如果您的代码仍无法正常工作,则可以尝试:

if(e.keyCode==13 || e.which == 13) // for cross browser

I think you have issues with your script stack order. you have to load jquery first then add your script below the specified library:

<script src="//code.jquery.com/jquery-latest.min.js"></script>
<script>
    $(document).ready(function(){
        $("#newtopic").keydown(function(e){ 
           if(e.keyCode==13){
                alert("Hello World");
           }
        }); 
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM