【问题标题】:need for loop with conditional continue需要带条件继续的循环
【发布时间】:2018-04-27 19:48:00
【问题描述】:

我需要根据复选框“继续”。

HTML:

<input type = "text" id = "E1" /><input type="checkbox" id="C1">
<input type = "text" id = "E2" /><input type="checkbox" id="C2">
<input type = "text" id = "E3" /><input type="checkbox" id="C3">
<input type = "text" id = "E4" /><input type="checkbox" id="C4">

JS:

for (var i=1; i<=4; i++) { 
if ("$C" +i.checked == true) {continue;} 
...;  
}

</script>

【问题讨论】:

  • 您的意思是如果选中复选框,则再次从零开始?
  • 没有。只是没有完成那个迭代。但继续下一项。
  • 你不能通过在他们的 id 前加上 $ 来访问你的对象
  • 最重要的是:你想达到什么目标?您想在哪里打印该输出?
  • @verlager 你的预期结果是什么?

标签: javascript jquery for-loop


【解决方案1】:

continue 是用于实现此目的的正确关键字。我怀疑您的问题是if 声明永远不会正确。

我想你想要这个:

// Add 1 to index first and then concatenate with "#C" to create jQuery id selector
if ($('#C' + (i + 1)).is(':checked')) { continue; }

您上面的if 语句在功能上等同于(选中时):

if ("$Ctrue" == true) { continue; }

$C 和布尔值的字符串值连接在一起。

【讨论】:

  • 如果有其他相关信息,请添加到您的原始帖子中,或在此处发表评论。
  • 这增加了太多的复杂性......难道没有简单解决方案吗?我愿意在输入字段上加标签。我写这篇文章只是因为它很简单。如果添加真实代码,没人会回答
  • 我没有增加任何复杂性,我只是更正了您的错误代码。你的 if 语句永远不会是真的。
  • 成功了!!谢谢你,凯文。
【解决方案2】:

试试这个。

document.getElementById("test").addEventListener("click", function(){
for (var i=1; i<=4; i++) { 
if($("#C"+i).is(':checked'))
continue;
console.log($("#E"+i).val())

}

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type = "text" id = "E1" /><input type="checkbox" id="C1">
<input type = "text" id = "E2" /><input type="checkbox" id="C2">
<input type = "text" id = "E3" /><input type="checkbox" id="C3">
<input type = "text" id = "E4" /><input type="checkbox" id="C4">

<button id="test" >Test</button>

在这种情况下,它不会记录那些没有选中相邻复选框的输入的值,例如

【讨论】:

  • 使用这种方法,我需要 48 个相同的“测试”ID。那不太行。
【解决方案3】:

$("#C" + i).prop('checked') 如果选中第 i 个复选框,则为您提供 true

注意:

  • 循环方式不好,因为如果添加/删除输入,您总是需要调整条件。
  • 当您从 1 开始时,您的 for 循环条件也已关闭

for (var i = 1; i < 5; i++) { 
  if ($("#C" + i).prop('checked'))
    continue;

  console.log(i + ' unchecked');
  // do some stuff
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type = "text" id = "E1" /><input type="checkbox" id="C1" checked>
<input type = "text" id = "E2" /><input type="checkbox" id="C2">
<input type = "text" id = "E3" /><input type="checkbox" id="C3">
<input type = "text" id = "E4" /><input type="checkbox" id="C4">

【讨论】:

    【解决方案4】:

    你应该做的是分解你的逻辑,这样你的程序就可读了,你可以用更高层次的术语来表达它

    function getCheckboxes() {
      const checkboxes = [];
      for (let i = 1; i <5; ++i) {
        checkboxes.push(document.getElementById(`E${i}`);
      }
      return checkboxes;
    }
    
    getCheckboxes()
      .filter(checkbox => !checkbox.checked)
      .forEach(checkbox => {
        // do something with an unchecked checkbook
      });
    

    【讨论】:

      【解决方案5】:

      您的循环永远不会运行,因为您的代码中没有事件侦听器。您需要将事件侦听器绑定到您的复选框输入。它看起来像这样:

      var checkbox = document.querySelector("input[name=checkbox]");
      
      checkbox.addEventListener( 'change', function() {
        if(this.checked) {
          // output button here
        }
      }
      

      【讨论】:

      • 那行不通。我有 48 个这样的复选框。先生,必须使用 ID,而不是姓名。
      • 他的问题甚至没有提到输出一个按钮,但它明确表示他想遍历项目并跳过检查的项目。
      猜你喜欢
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 2012-09-16
      • 2020-11-29
      • 2023-01-11
      • 1970-01-01
      相关资源
      最近更新 更多