【问题标题】:Limiting checked checkbox function not working as expected限制选中的复选框功能无法按预期工作
【发布时间】:2019-07-02 11:27:09
【问题描述】:

我试图在我的复选框问题中实现一个功能,其中只能检查 3 个复选框。但是,它仍然会继续检查超出限制 (3)。

我确定这与 latestcheck.checked = false;

有关

我的打字稿功能:

factors(whichlist, maxchecked, latestcheck) {
// An array containing the id of each checkbox to monitor. 
var listone = new Array("teamStrength", "marketOp", "productOff", "technology", "financialPerform", "coinvestors", "mediaExpo", "awardsWon", "portfolioFit");

// End of customization.
var iterationlist;
eval("iterationlist=" + whichlist);
var count = 0;
for (var i = 0; i < iterationlist.length; i++) {
  if ((<HTMLInputElement>document.getElementById(iterationlist[i])).checked == true) { 
    count++;

  }
  if (count > maxchecked) { 
    latestcheck.checked = false; 
    console.log("last checked: " + latestcheck.checked); 
  }
}
if (count > maxchecked) {
  alert('Sorry, only ' + maxchecked + ' may be checked.');
}

}

需要做的是,弹出警报后,我选中的复选框(限制为3之后,所以第四个复选框)将被取消选中。

【问题讨论】:

    标签: html typescript checkbox


    【解决方案1】:

    你不能这样做吗? (我不做 TypeScript)

    const listOne = ["teamStrength", "marketOp", "productOff", "technology", "financialPerform", "coinvestors", "mediaExpo", "awardsWon", "portfolioFit"];    
    const container = document.getElementById("checkboxContainer");
    container.addEventListener("click",function(e) {
      if (listOne.indexOf(e.target.id) !=-1) { // may not even be needed
        if (container.querySelectorAll("input[type=checkbox]:checked").length > 3) {
          e.preventDefault(); // or this.checked=false;
        }
      }
    });
    

    简化:

    document.querySelectorAll(".question").forEach(function(q) {
      q.addEventListener("click", function(e) {
        var len = this.querySelectorAll("input[type=checkbox]:checked").length;
        if (len > 3) {
          e.preventDefault();
        }
      });
    });
    <div class="question">
      <input type="checkbox" />
      <input type="checkbox" />
      <input type="checkbox" />
      <input type="checkbox" />
      <input type="checkbox" />
    </div>
    <div class="question">
      <input type="checkbox" />
      <input type="checkbox" />
      <input type="checkbox" />
      <input type="checkbox" />
      <input type="checkbox" />
    </div>

    【讨论】:

    • id 不必是唯一的吗?如果是,我将如何实现 checkBoxContainer,因为每个复选框都需要自己的唯一 ID?
    • 由于您没有发布您的 html,我只是猜测所有复选框的父项的 ID。它也可以是 document.body 。我脚本中的容器是最近的包含所有复选框的容器。如果页面上没有其他复选框,那么我们甚至不需要数组。那么if (document.querySelectorAll("input[type=checkbox]:checked").length &gt; 3) 是一个足够好的测试
    • 我的 html 包含一个问题,该问题共有 9 个复选框选项。其中之一是:&lt;label class="checkbox-inline"&gt; &lt;input type="checkbox" id="productOff" class="checkbox" name="factors" (click)="factors('listOne',3,this)" value="3" (change)="onCheckArray($event, Form.value.discussionMode)"&gt; Product/service offering &lt;/label&gt;
    • @mhfour 只需在 e.preventDefault() 之后添加警报,因为您需要一些警报。
    猜你喜欢
    • 1970-01-01
    • 2012-02-18
    • 2020-02-26
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多