JavaScript,retreive值进入复选框

时间:2017-12-07 06:50:21

标签: javascript checkbox

我的for循环很好,我得到了transportationOutputtrasportation2的值,我想检查两个值是否相等,选中右侧复选框输入,但是如果条件没有进入我不知道为什么,我尝试将==替换为===,但也不起作用

trasportation = "car-train" //value from database
var trasportation2 = document.getElementsByName("transportation");
var transportationOutput = trasportation.split("-");


for (var i = 0; i < transportationOutput.length; i++) {
  for (var j = 0; j < trasportation2.length; j++) {
    if (trasportation2[j].value == transportationOutput[i]) {
      trasportation2[j].checked = true;
    }
  }
}
<label>transportation</label>

<input type="checkbox" id="localTransportID" name="transportation" value="car"> car
<input type="checkbox" id="localTransportID" name="transportation" value="bus"> bus
<input type="checkbox" id="localTransportID" name="transportation" value="train"> train
<input type="checkbox" id="localTransportID" name="transportation" value="airplane"> airplane

0 个答案:

没有答案