将javascript绑定到提交按钮时出现问题


having problems binding javascript to a submit button

我在将此脚本绑定到提交按钮时遇到问题。。。

$('input').on('keydown', function(event) {
   var x = event.which;
   if (x === 13) {
      event.preventDefault();
   }
});

我以前也做过,但已经很长时间了,网上的例子都不适合我。谢谢:)

jQuery(function($) { // DOM is now ready
  // your code here
});

应该做到这一点
https://learn.jquery.com/using-jquery-core/document-ready/

$(document).ready(function(){
   $('input').on('keydown', function(e) {
      if (e.keyCode == 13 || e.which == 13) {
         e.preventDefault();
      }
   });
})

请尝试此

或者您可以使用IIFE 来尝试此代码

(function($) {
  $('input').on('keydown', function(e) {
    if (e.keyCode == 13 || e.which == 13) {
      e.preventDefault();
    }
  });
})(jQuery);