disable enter key on specific textboxes

前端 未结 9 773
北荒
北荒 2020-12-30 18:25

I was looking for a way to do this, got a few scripts, but none of them is working for me.

When I hit enter in my textboxes, it shouldn\'t do anything.

I hav

相关标签:
9条回答
  • 2020-12-30 19:23

    If you want to prevent Enter key for specific textbox then use inline js.

    <input type="text" onkeydown="return (event.keyCode!=13);"/>
    
    0 讨论(0)
  • 2020-12-30 19:27
    <script type="text/javascript">
    
       function stopRKey(evt) { 
         var evt = (evt) ? evt : ((event) ? event : null); 
         var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
         if ((evt.keyCode == 13) && (node.type=="text"))  {return false;} 
       } 
    
    
    document.onkeypress = stopRKey; 
    
    </script>
    

    http://webcheatsheet.com/javascript/disable_enter_key.php

    0 讨论(0)
  • 2020-12-30 19:29

    Use global selector $('input')...if you use multiple text boxes on page the ID ('#comment') will cause problems. And when using dynamic content bind it using .live e.g.

    $('input:not(textarea)').live('keypress',function(e) {
        if (e.which == 13) return false;
        if (e.which == 13) e.preventDefault();
    });
    

    (Disable enter for text boxes and leaves enter enabled for text areas)

    0 讨论(0)
提交回复
热议问题