jQuery AddClass 方法不起作用

jQuery AddClass method not working

本文关键字:不起作用 方法 AddClass jQuery      更新时间:2023-09-26

在检查其他表单元素是否已填充后,我正在尝试将一个新类添加到"提交"输入中。我正在使用 .keyup() 来触发函数。此函数将属性"禁用"更改为"false",但之后它不会添加类" 动画脉冲 "。谢谢!

<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() 函数,因为这个函数不返回任何内容,并且需要从jQuery对象调用addClass()函数。

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

退货:任何

这种做法称为链接,为了执行链接函数,这些函数应返回jQuery对象。

更多信息

你可以通过这样重写代码行:

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

单独添加 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);
  }
  });
 });