第二个选择第一个选定的下拉列表不显示:无;

时间:2013-09-05 12:29:38

标签: javascript

JS

function getData(dropdown) {
    var value = dropdown.options[dropdown.selectedIndex].value;
    if (value == 'emlak'){
    document.getElementById("emlak").style.display = "block";
    }
    if(value == 'vasita'){
    document.getElementById("vasita").style.display = "block";
    }
}

HTML

<select name="kategori" onChange="getData(this);">
     <option value="hat" onClick="">Hat</option>
     <option value="emlak">Shirt</option>
     <option value="vasita">Pants</option>
</select>
<select id="emlak" name="currentList" onChange=";" style="display:none;">
     <option value="hat">Hat</option>
     <option value="emlak">Shirt</option>
     <option value="pants">Pants</option>
</select>
<select id="vasita" name="currentList" onChange="" style="display:none;">
     <option value="hat">Otomobil</option>
     <option value="emlak">Shirt</option>
     <option value="pants">Pants</option>
</select>
  

当我选择第二次选择选项时,第一次选择的选项将不会显示:无;它应该只显示一个选择

3 个答案:

答案 0 :(得分:0)

因为您没有将其设置为none。这就是原因。

function getData(dropdown) {
    var value = dropdown.options[dropdown.selectedIndex].value;
    if (value == 'emlak'){
    document.getElementById("emlak").style.display = "block";  //where is the none for the other element?
    }
    if(value == 'vasita'){
    document.getElementById("vasita").style.display = "block"; //where is the none for the other element?
    }
}

基本理念:

function getData(dropdown) {
    var value = dropdown.options[dropdown.selectedIndex].value;
    var isEmlak = (value == 'emlak');
    document.getElementById("emlak").style.display = isEmlak ? "block" : "none";
    document.getElementById("vasita").style.display = !isEmlak ? "block" : "none";
}

答案 1 :(得分:0)

这里引用了JSBin

function getData(dropdown) { 
  var value = dropdown.options[dropdown.selectedIndex].value;
  if (value == 'emlak'){
    document.getElementById("emlak").style.display = "block";
    document.getElementById("vasita").style.display = "none"; 
  }
  if(value == 'vasita'){
    document.getElementById("vasita").style.display = "block";
    document.getElementById("emlak").style.display = "none";  
  }
}

答案 2 :(得分:-1)

更改脚本如下所示:

function getData(dropdown) {
    var value = dropdown.options[dropdown.selectedIndex].value;
    if (value == 'emlak'){
        document.getElementById("emlak").style.display = "block";
        document.getElementById("vasita").style.display = "none";
    }

    if(value == 'vasita'){
        document.getElementById("vasita").style.display = "block";
        document.getElementById("emlak").style.display = "none";
    }

    if(value == 'hat'){
        document.getElementById("vasita").style.display = "none";
        document.getElementById("emlak").style.display = "none";
    }
}