如何动态地从下拉列表中添加和删除复选框

时间:2016-08-23 05:04:12

标签: javascript

我正在尝试使用复选框创建一个下拉列表,其中下拉列表中的值取决于外部复选框。

这就是我现在所拥有的。

http://jsfiddle.net/m1hawe54/

HTML

<div class="region-checkboxes">
  <input type="checkbox" name="na" value="NA" />
  <span style="width:25px;display:inline-block;">NA</span>
  <input type='checkbox' name="eu" value="EU" />
  <span style="width:25px;display:inline-block;">EU</span>
  <input type='checkbox' name="apac" value="APAC" />
  <span style="width:40px;display:inline-block;">APAC</span>
  <input type='checkbox' name="cn" value="CN"/>
  <span style="width:25px;display:inline-block;">CN</span>


JS

  var allvalue = []
  var checkboxes = document.querySelectorAll('.region-checkboxes input');

  checkboxes.forEach(function(item) {
  item.addEventListener('click', onChangeCheckbox);
  });
  function onChangeCheckbox (checkbox) {
  console.log(checkbox.currentTarget.value);
        if (checkbox.currentTarget.checked) {
            allvalue.push(checkbox.currentTarget.value);
            allvalue.sort();
        }
        else {
            var index = allvalue.indexOf(checkbox.currentTarget.value);
            if(index > -1){
            allvalue.splice(index,1);
            }
        }
    }

我要做的是基于所选的复选框,它将填充带有其他值的复选框下拉列表。在此示例中,它将根据您选择的区域填充子区域。

如果选中“EU”复选框,如果选中“EU”复选框,那么下拉列表将填充[“NA”,“LA”],[“WEU”,“EEU”,“RU”],如果同时选择它们,它将使用两个数组填充下拉列表。这就是我的想法。

http://jsfiddle.net/evfnLn0x/712/

所以现在的问题是。我不知道如何设置我的代码,因此它是动态的。当外部复选框被取消选中时,我还需要下拉复选框列表来删除子区域。我整晚都在苦苦挣扎。

1 个答案:

答案 0 :(得分:1)

我会做类似下面的事情:

var allvalue = []
var checkboxes = document.querySelectorAll('.region-checkboxes input');

for (var k = 0; k < checkboxes.length; k++) {
  checkboxes[k].addEventListener('click', onChangeCheckbox);
};

// Does not work in Firefox
//checkboxes.forEach(function(item) {
//item.addEventListener('click', onChangeCheckbox);
//});

function onChangeCheckbox(checkbox) {
  console.log(checkbox.currentTarget.value);
  if (checkbox.currentTarget.checked) {
    allvalue.push(checkbox.currentTarget.value);
    allvalue.sort();
  } else {
    var index = allvalue.indexOf(checkbox.currentTarget.value);
    if (index > -1) {
      allvalue.splice(index, 1);
    }
  }
  renderDropdown();
}

function renderDropdown() {
  // will be called on each click on any checkboxes
  var div = document.querySelector("#dropdown")
  var select = document.createElement("select");
  for (var i = 0; i < allvalue.length; i++) {
    // get subregions from the below dropdown object mapping
    var subRegion = dropdown[allvalue[i]];
    if (subRegion && subRegion.length) {
      for (var j = 0; j < subRegion.length; j++) {
        var option = document.createElement("option");
        option.value = subRegion[j];
        option.selected = "";
        option.innerHTML = subRegion[j];
        select.appendChild(option);
      }
      div.innerHTML = "";
      div.appendChild(select);
    }

  }

}

var dropdown = {
  "NA": ["NA", "LA"],
  "EU": ["WEU", "EEU", "RU"],
  "APAC": ["KR", "TW", "SG", "ANZ", "JP", "CN"]
}
.dropdown-check-list {
  display: inline-block;
}
.dropdown-check-list .anchor {
  position: relative;
  cursor: pointer;
  display: inline-block;
  padding: 5px 50px 5px 10px;
  border: 1px solid #ccc;
}
.dropdown-check-list .anchor:after {
  position: absolute;
  content: "";
  border-left: 2px solid black;
  border-top: 2px solid black;
  padding: 5px;
  right: 10px;
  top: 20%;
  -moz-transform: rotate(-135deg);
  -ms-transform: rotate(-135deg);
  -o-transform: rotate(-135deg);
  -webkit-transform: rotate(-135deg);
  transform: rotate(-135deg);
}
.dropdown-check-list .anchor:active:after {
  right: 8px;
  top: 21%;
}
.dropdown-check-list ul.items {
  padding: 2px;
  display: none;
  margin: 0;
  border: 1px solid #ccc;
  border-top: none;
}
.dropdown-check-list ul.items li {
  list-style: none;
}
<body>

  <div class="region-checkboxes">
    <input type="checkbox" name="na" value="NA" />
    <span style="width:25px;display:inline-block;">NA</span>
    <input type='checkbox' name="eu" value="EU" />
    <span style="width:25px;display:inline-block;">EU</span>
    <input type='checkbox' name="apac" value="APAC" />
    <span style="width:40px;display:inline-block;">APAC</span>
    <input type='checkbox' name="cn" value="CN" />
    <span style="width:25px;display:inline-block;">CN</span>
  </div>
  <div id="dropdown"></div>
</body>

<强>更新

Working Fiddle

enter image description here