【问题标题】:Div. Toggle with button click [duplicate]分区。用按钮点击切换[重复]
【发布时间】:2021-12-11 15:00:01
【问题描述】:

我正在处理一个表单,并希望在单击不同按钮时显示不同的 div,但无法使其工作我使用了带有引导程序的数据切换,但它同时显示了两个 div

这第一张图片是当我点击按月计费时发生的情况

在第二张图片中,当我点击按年计费时,第一个 div 也会显示,但我希望只有第二个 div 可见

当您首先单击按月计费,然后单击按年计费时,两个 div 都是可见的,我只希望一个应该可见,例如,如果您首先单击按月计费,则与其相关的 div 应该显示,如果您单击按年计费接下来,按月计费的 div 应该隐藏,只有按年计费的 div 应该是可见的。

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<div class="row justify-content-evenly">

  <div class="col d-grid gap-2 col-3 mt-4 mb-3">
    <button type="button" class="btn btn-outline-primary" id="billed-monthly" value="monthly" style="border: solid 1px grey;" data-bs-toggle="collapse" data-bs-target="#billmonthly" aria-expanded="false" aria-controls="collapseExample">Billed Monthly</button>

  </div>
  <div class="col d-grid gap-2 col-3 mt-4 mb-3">
    <button type="button" class="btn btn-outline-primary" id="billed-yearly" value="yearly" style="border: solid 1px grey;" data-bs-toggle="collapse" data-bs-target="#billyearly" aria-expanded="false" aria-controls="collapseExample">Billed Yearly</button>
  </div>
</div>

<div class="row mt-2 collapse" id="billmonthly">
  <div class=" col d-flex justify-content-end">
    <h3>Billed now: 199</h3>
  </div>
</div>
<div class="row mt-2 collapse" id="billmonthly">
  <div class=" col d-flex justify-content-end">
    <p>By clicking "Start Premium Plan", you agree to be charge 199 every<br> month, unless you cancel. You acknowledge that refunds won't be<br> available on cancellation.</p>
  </div>
</div>
<div class="row mt-2 collapse" id="billyearly">
  <div class=" col d-flex justify-content-end">
    <h3>Billed now: 2030</h3>
  </div>
</div>
<div class="row mt-2 collapse" id="billyearly">
  <div class=" col d-flex justify-content-end">
    <p>By clicking "Start Premium Plan", you agree to be charge 2030 every<br> year, unless you cancel. You acknowledge that refunds won't be<br> available on cancellation.</p>
  </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

【问题讨论】:

标签: javascript html jquery css twitter-bootstrap


【解决方案1】:

我为您创建了一个演示来获取您的功能。请注意,我没有使用 Bootstrap。它是完整的 jQuery 方法。

HTML

<a href="#" id="billNow">Bill Now </a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#" id="billYearly">Bill Yearly </a><br/><br/>
<div id="showdetails">
    <div id="billnowdetails">
        <p>details of Bill now</p>
    </div>
    <div id="billyeardetails">
    <p>details of Bill Yearly</p></div>
</div>

JAVASCRIPT/JQUERY

<script>
$(document).ready(function(){
let billnow = $("#billnowdetails").remove();
let billyear = $("#billyeardetails").remove();
    $("#billNow").on("click",function(e){
        e.preventDefault();
        $("#showdetails").find("#billyeardetails").remove();
        $("#showdetails").append(billnow);
    });
    
    $("#billYearly").on("click",function(e){
        e.preventDefault();
        $("#showdetails").find("#billnowdetails").remove();
        $("#showdetails").append(billyear);
    });
});
</script>

注意:如果您想检查结果。您可以在此处复制并粘贴代码https://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_lib_google

【讨论】:

  • 这太可怕了。当您可以切换显示时,为什么要从 DOM 中删除? w3schools 也是一个贫乏的资源
  • 是的,我正在删除它,因为只需要其中一个。进一步正如你所说的崩溃。崩溃只是折叠你的 div 而不是隐藏。这里的功能是只显示相应的分区。
  • @mplungjan 以这种方式hide 比使用折叠更合适。我已经在我的回答中强调了 我使用的是纯 jquery 而不是引导程序。 还表示这是一个艰难的方式。
猜你喜欢
  • 2011-09-18
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 2014-09-18
  • 2016-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多