勾选所有其他切换按钮时,我的切换按钮未启用

My Toggle Button doesn't get enabled when All other Switch Buttons are Ticked

提问人:Augustine 提问时间:11/15/2023 最后编辑:ducksteryAugustine 更新时间:11/17/2023 访问量:62

问:

正如您在我包含的 JSFiddle 中看到的那样,我正在尝试在勾选所有其他切换/开关后启用切换按钮,它肯定适用于那里的简单提交按钮显示,但不是那么多的切换显示挂起/完成(挂起(橙色),(完成(绿色)。 任何帮助都是值得赞赏的。

$(document).on("change keyup", ".required", function(e) {
  let Disabled = $(".required").length;
  $(".required").each(function() {
    if (this.checked) Disabled--;
  });

  if (Disabled) {
    $(".toggle-disabled").prop("disabled", true);
  } else {
    $(".toggle-disabled").prop("disabled", false);
  }
});
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/css/bootstrap5-toggle.min.css" rel="stylesheet" />
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/js/bootstrap5-toggle.ecmas.min.js"></script>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js"></script>

</head>
<div class="container">
  <form action="#">
    <div class="container">
      <div class="form-check form-switch">
        <input class="form-check-input filled-in box required" type="checkbox" role="switch" id="flexSwitchCheckDefault">
        <label class="form-check-label" for="flexSwitchCheckDefault">Gmail</label>
      </div>
      <div class="form-check form-switch">
        <input class="form-check-input filled-in box required" type="checkbox" role="switch" id="flexSwitchCheckDefault">
        <label class="form-check-label" for="flexSwitchCheckDefault">Pika</label>
      </div>
      <div class="form-check form-switch">
        <input class="form-check-input filled-in box required" type="checkbox" role="switch" id="flexSwitchCheckDefault">
        <label class="form-check-label" for="flexSwitchCheckDefault">Justice</label>
      </div>
      <!--A Simple Submit Works Fine -->
      <div class="form-check form-switch">
        <button type="button" class="toggle-disabled" disabled>Submit</button>
      </div>
    </div><br>
    <!--Toggle button doesn't work -->
    <input type="checkbox" class="toggle-disabled" data-toggle="toggle" data-size="small" data-onlabel="Done" data-offlabel="Pending" data-onstyle="success" data-offstyle="warning" disabled>

</form>
</div>

enter image description here

javascript jquery css twitter-bootstrap

评论

0赞 Chris Barr 11/15/2023
我已经编辑了您的问题,以便您的代码现在是一个可以直接从您的问题运行的代码片段。您现在可能不需要 JSFiddle 链接
0赞 Augustine 11/15/2023
我很感激它@chris巴尔,但它没有显示我试图获得帮助的橙色切换按钮。正常提交和/或复选框工作正常,如您所见......由于某些原因,切换/开关没有。.
0赞 Chris Barr 11/15/2023
指向 CSS 和 JS 文件的链接中包含空格/换行符,它们会导致 404 错误。有人建议进行编辑,我已经批准了它,但它需要更多的投票。或者你可以自己解决这个问题,因为这是你的问题
0赞 Augustine 11/15/2023
我刚刚尝试过 jsfiddle.net/alachgar/q2naydfw/14 它在我这边工作得很好!我在没有登录的情况下在 Edge 和 Firefox 上尝试过,没有 404 ?你能再次确认吗,请?!!
0赞 Chris Barr 11/15/2023
我在 StackOverflow 上谈论这里。对于简单的演示,您不需要将人们链接到像 JSFiddle 这样的外部网站。StackOverflow 内置了代码片段。请编辑您的问题以从链接文件中删除多余的空格/行,此问题中的代码片段将起作用

答:

2赞 duckstery 11/15/2023 #1

我已经修复了@chris条编辑以显示您的橙色开关。对不起,我没有声誉发表评论。

你的问题是你如何动态启用/禁用.toggle-disabled

$(document).on("change keyup", ".required", function(e) {
  let Disabled = $(".required").length;
  $(".required").each(function() {
    if (this.checked) Disabled--;
  });

  if (Disabled) {
    $(".toggle-disabled").prop("disabled", true);
    $(".toggle-disabled")["1"].bootstrapToggle('disable');
  } else {
    $(".toggle-disabled").prop("disabled", false);
    $(".toggle-disabled")["1"].bootstrapToggle('enable');
  }
});
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/css/bootstrap5-toggle.min.css" rel="stylesheet" />
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/js/bootstrap5-toggle.ecmas.min.js"></script>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js"></script>

</head>
<div class="container">
  <form action="#">
    <div class="container">
      <div class="form-check form-switch">
        <input class="form-check-input filled-in box required" type="checkbox" role="switch" id="flexSwitchCheckDefault">
        <label class="form-check-label" for="flexSwitchCheckDefault">Gmail</label>
      </div>
      <div class="form-check form-switch">
        <input class="form-check-input filled-in box required" type="checkbox" role="switch" id="flexSwitchCheckDefault">
        <label class="form-check-label" for="flexSwitchCheckDefault">Pika</label>
      </div>
      <div class="form-check form-switch">
        <input class="form-check-input filled-in box required" type="checkbox" role="switch" id="flexSwitchCheckDefault">
        <label class="form-check-label" for="flexSwitchCheckDefault">Justice</label>
      </div>
      <!--A Simple Submit Works Fine -->
      <div class="form-check form-switch">
        <button type="button" class="toggle-disabled" disabled>Submit</button>
      </div>
    </div><br>
    <!--Toggle button doesn't work -->
    <input type="checkbox" class="toggle-disabled" data-toggle="toggle" data-size="small" data-onlabel="Done" data-offlabel="Pending" data-onstyle="success" data-offstyle="warning" disabled>
</form>
</div>

您需要使用它们来实际启用/禁用它们

// To disable
$(".toggle-disabled")["1"].bootstrapToggle('disable');
// To enable
$(".toggle-disabled")["1"].bootstrapToggle('disable');

因为会在您的输入旁边插入他们的实际组件。所以修改你的禁用属性不会修改Bootstrap ToggleinputBootstrap Toggle

评论

0赞 Augustine 11/15/2023
美妙!谢谢@duckstery,非常感谢!
0赞 Augustine 11/15/2023
对不起,但是当我删除我不需要的“提交”按钮部分时,整个事情都中断了,它只是为了表明“提交”有效。有没有办法让它只用切换器工作?我试图修改我的脚本,但没有运气。
0赞 duckstery 11/15/2023
如果删除提交按钮,请尝试将索引从 到 修复。 将获取所有具有类的元素。您有 2 个具有该类的标签(哪个提交按钮是第一个)。因此,现在当您删除它时,所选标签中将没有["1"]["0"]$(".toggle-disabled")toggle-disabled["1"]
0赞 Augustine 11/15/2023
感谢您到目前为止的工作,感谢您的及时回复。
0赞 Augustine 11/30/2023
现在,当我包含我的php cde时,状态按钮不再启用...它一直有效,直到我添加 </tr><?php } ?> 然后它就坏了!有什么想法吗?谢谢。