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

时间:2016-03-30 02:12:54

标签: javascript php html

我在将此脚本绑定到提交按钮时出现问题...

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

   if (x === 13) {
      event.preventDefault();
   }
});

我之前已经完成了,但是已经很长时间了,网上的例子并没有为我做。谢谢:))

3 个答案:

答案 0 :(得分:1)

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

应该做的伎俩 https://learn.jquery.com/using-jquery-core/document-ready/

答案 1 :(得分:0)

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

请试试这个

答案 2 :(得分:-2)

或者您可以使用IIFE

尝试此代码
(function($) {
  $('input').on('keydown', function(e) {
    if (e.keyCode == 13 || e.which == 13) {
      e.preventDefault();
    }
  });
})(jQuery);
相关问题