如何以编程方式禁用select元素中的选项?

时间:2017-03-28 07:58:39

标签: javascript html

在下面的代码段中,当用户从第一个下拉菜单中选择“经济”或“第一”课程时,我希望在第二个下拉列表中仅启用相应的座位选项。因此,如果用户在第一个下拉列表中选择“经济”类,则应在第二个下拉列表中禁用第一类座位选项,反之亦然。有没有办法以编程方式禁用Dim oShell Set oShell = WScript.CreateObject ("WScript.Shell") oShell.Run "taskkill /im chrome.exe", 0, True 中的这些<option>?如果是这样,怎么样?

<select>
var $x=document.getElementsByClassName("b").innerHTML;
var $y=document.getElementsByClassName("economy").innerHTML;
var $z=document.getElementsByClassName("first").innerHTML;
function select() {
    console.log("$x ==> ", $x);
    console.log("$y ==> ", $y);
    console.log("$z ==> ", $z);
}
select();
.fontstyle {
    font-family: "calibri";
}

1 个答案:

答案 0 :(得分:0)

它应该是这样的

<select id="selector" onchange="myFunction(this.options[this.selectedIndex].getAttribute('class'));">
<option>Select</option>
<option disabled>Economy class </option> //economy class seats
<option class="economy">1</option>
<option class="economy" >2</option>
<option class="economy" >3</option>
<option class="economy" >4</option>
<option class="economy" >5</option>
<option disabled>First class </option>    // first class seats
<option class="first" >6</option>
<option class="first" >7</option>
<option class="first" >8</option>
<option class="first" >9</option>
<option class="first" >10</option>
</select>

<script>
function myFunction(val) {
    var op = document.getElementById("selector").getElementsByClassName(val);
    for (var i = 0; i < op.length; i++) {
        op[i].disabled = true;
    }
}
</script>