单击时禁用或激活默认卡片 Jquery

Default Cards Disable or Activated on Click Jquery

我一直在尝试使用不同的方法来编写此 jQuery 部分的代码,但它不起作用。我确实有一张卡片列表,其中第一张卡片具有默认值 class。目标是每次单击“设置为默认值”按钮时,我都可以在父“bgCreditcard”上设置 class“默认值”并同时禁用该按钮。

我的问题是我尝试了 toggleClass,但它使用“设置为默认值”按钮激活了两张卡。我试图禁用道具,但也没有运气。有什么想法吗?

这是我的 HTML:

<div class="row">
     <div class="col-12 colg-md-6 card bgCreditcard active">
        <div class="card-body">
             <div class="card-text">
                  <p>Card 1</p>
                    <button class="bg-btn bg-default bg-btn-disabled" disabled>Default</button>
             </div>
         </div>
     </div>
     <div class="col-12 colg-md-6 card bgCreditcard">
            <div class="card-body">
                  <div class="card-text">
                       <p>Card 2</p>
                       <button class="bg-btn bg-default bg-btn-default">Set as Default</button>
                  </div>
             </div>
      </div>
      <div class="col-12 colg-md-6 card bgCreditcard">
           <div class="card-body">
                <div class="card-text">
                     <p>Card 3</p>
                     <button class="bg-btn bg-default bg-btn-default">Set as Default</button>
                </div>
           </div>
      </div>
</div>

Js:

$('.bg-default').click(function(){
    $('.bgCreditcard').each(function(){
        $(".active").removeClass("active");
        $(this).addClass("active");
    });
});

您可以试试下面的代码:

HTML:

<div class="row">
     <div class="col-12 colg-md-6 card bgCreditcard active">
        <div class="card-body">
             <div class="card-text">
                  <p>Card 1</p>
                    <button class="bg-btn bg-default" disabled>Default</button>
             </div>
         </div>
     </div>
     <div class="col-12 colg-md-6 card bgCreditcard">
            <div class="card-body">
                  <div class="card-text">
                       <p>Card 2</p>
                       <button class="bg-btn bg-default bg-btn-default">Set as Default</button>
                  </div>
             </div>
      </div>
      <div class="col-12 colg-md-6 card bgCreditcard">
           <div class="card-body">
                <div class="card-text">
                     <p>Card 3</p>
                     <button class="bg-btn bg-default bg-btn-default">Set as Default</button>
                </div>
           </div>
      </div>
</div>

jQuery:

$(document).on('click', '.bg-btn' ,function(){

  $('.bgCreditcard').removeClass('active');
  $('.bgCreditcard').find('button').prop('disabled', false)
  $('.bgCreditcard').find('button').removeClass('bg-btn-disabled')
   $(this).closest('.bgCreditcard').addClass('active');
  
   
   if($(this).closest('.bgCreditcard').hasClass('active')) {
     $(this).prop('disabled', true);
     $(this).addClass('bg-btn-disabled')
   }
});

考虑以下因素。

$(function() {
  function clearDefault(card) {
    $(card).removeClass("active");
    $("button", card).prop("disabled", false).toggleClass("bg-btn-default bg-btn-disabled").html("Set as Default");
  }

  function addDefault(card) {
    $(card).addClass("active");
    $("button", card).prop("disabled", true).addClass("bg-btn-disabled").html("Default");
  }

  function setDefault(event) {
    clearDefault($(".bgCreditcard.active"));
    addDefault($(event.target).closest(".bgCreditcard"));
  }

  $(".bgCreditcard button").click(setDefault);
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
  <div class="col-12 colg-md-6 card bgCreditcard active">
    <div class="card-body">
      <div class="card-text">
        <p>Card 1</p>
        <button class="bg-btn bg-default bg-btn-disabled" disabled="true">Default</button>
      </div>
    </div>
  </div>
  <div class="col-12 colg-md-6 card bgCreditcard">
    <div class="card-body">
      <div class="card-text">
        <p>Card 2</p>
        <button class="bg-btn bg-default bg-btn-default">Set as Default</button>
      </div>
    </div>
  </div>
  <div class="col-12 colg-md-6 card bgCreditcard">
    <div class="card-body">
      <div class="card-text">
        <p>Card 3</p>
        <button class="bg-btn bg-default bg-btn-default">Set as Default</button>
      </div>
    </div>
  </div>
</div>