-2

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>

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

4

3 回答 3

0

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

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";
}
于 2013-09-05T12:34:00.827 回答
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";  
  }
}
于 2013-09-05T12:34:51.040 回答
-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";
    }
}
于 2013-09-05T12:41:51.747 回答