首页 > 解决方案 > 需要带条件继续的循环

问题描述

我需要基于复选框“继续”。

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>

标签: javascriptjqueryfor-loop

解决方案


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和布尔值的字符串值连接在一起。


推荐阅读