jQuery AddClass 方法无效

jQuery AddClass method not working

在检查其他表单元素是否已填满后,我正在尝试将新的 class 添加到 "submit" 输入。我使用 .keyup() 来触发该功能。此函数将 属性 "disabled" 更改为 "false",但之后它不会添加 class “动画脉冲”。谢谢!

<script> //checks that all fields are completed before enabling submit button
  $(document).ready(function() {
   $('input[type="submit"]').prop('disabled', true);
   $('input[type="text"],input[type="email"],input[type="password"]').keyup(function() {

      if($('#log_password').val() != '' && $('#log_email').val() != '') {
         $('input[type="submit"]').prop('disabled', false).addClass("animated pulse");
      }
      else{

        $('input[type="submit"]').prop('disabled',true);

      }
   });
  });
  </script>

你的问题是你如何使用 prop() 函数,因为这个函数没有 return 任何东西,并且需要从 jQuery 调用 addClass() 函数对象。

正如您在 prop 定义中看到的那样:

Returns: Anything

这种做法称为 Chaining,为了执行 Chaining 函数,这些函数应该 return 一个 jQuery 对象。

More info

您可以像这样重写您的代码行:

$('input[type="submit"]')
  .addClass('animated pulse')
  .prop('disabled',  false);

Add seperately the addclass()

  $(document).ready(function() {
  $('input[type="submit"]').prop('disabled', true);


  $('input[type="text"],input[type="email"],input[type="password"]').keyup(function() {

  if($('#log_password').val() != '' && $('#log_email').val() != '') 
  {
        $('input[type="submit"]').prop('disabled', false);
      //add in next line
     $('input[type="submit"]').addClass('animated pulse');
  }
  else{

    $('input[type="submit"]').prop('disabled',true);

  }
  });
 });