javascript - 带有 && 和 || 的 IF ELSE 语句状况

标签 javascript if-statement checkbox

我有一组复选框(id =“first”id =“second”)和主复选框(id =“main”)。

<input type='checkbox' id="main_button" onclick="Indeterminate()"><label for="main_button">Main checkbox of group</label> 
<input type='checkbox' id="first" onclick="Indeterminate()"><label for="first">First thing</label>
<input type='checkbox' id="second" onclick="Indeterminate()"><label for="second">Second thing</label>

如果选中了一个或多个组复选框,则主要的条件处于不确定状态。如果全部选中,则主复选框也已选中条件。

function Indeterminate() {
    if (document.getElementById('first').checked || document.getElementById('second').checked) {
        document.getElementById('main_button').indeterminate = true;
    } else if (document.getElementById('first').checked && document.getElementById('second').checked) {
        document.getElementById('main_button').checked;
    } else { 
        document.getElementById('main_button').indeterminate = false; 
    }    
}

在我的 IF ELSE 语句中,条件 IF 和 ELSE 有效,但 ELSE IF 出现问题。可能犯了一个简单的错误或者?谢谢!

JSFiddle example

最佳答案

var main = document.getElementById('main_button');
var first = document.getElementById('first');
var second = document.getElementById('second');

function Indeterminate() {
  if (first.checked && second.checked) {
    main.checked = true;
    main.indeterminate = false;
  } else if (first.checked || second.checked)
    main.indeterminate = true;
  else
    main.indeterminate = false;

}
<input type='checkbox' id="main_button" onclick="Indeterminate()">
<label for="main_button">Main checkbox of group</label>
<input type='checkbox' id="first" onclick="Indeterminate()">
<label for="first">First thing</label>
<input type='checkbox' id="second" onclick="Indeterminate()">
<label for="second">Second thing</label>

关于javascript - 带有 && 和 || 的 IF ELSE 语句状况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40972155/

相关文章:

javascript - jquery 变量似乎在第一次单击后永远不会被设置

PHP 事件源继续执行

java - 如果第一个为假,则一个 if 语句中的两个条件是否第二个重要?

c# - 我怎样才能在转发器中做一个 if 语句

c# - if 子句使用?

Javascript,隐藏复选框未选中的 div 时的函数,请记住页面刷新

javascript - 如何在 google map api v3 上添加切换按钮?

javascript - 谷歌自定义搜索确定何时加载脚本

C# WPF 数据绑定(bind)到未知数量的复选框

checkbox - knockout JS :- Label bind with delete button