0

I want to make it so that when F is chosen the Floor combo box only shows A1 and B1 and when K is selected only A2 and B2 shows.

Garage     : <select name="garage">
<option value="F">F</option>
<option value="K">K</option>
</select><br />
Floor      : <select name="floor">
<option value="A1">A1</option>
<option value="B1">B1</option>
<option value="A2">A2</option>
<option value="B2">B2</option>
</select><br />
4

2 回答 2

0

如果您希望立即做出选择,请使用 javascript。

html:

Garage     : <select name="garage" id="garage">
<option value="F">F</option>
<option value="K">K</option>
</select><br />
Floor      : <select name="floor" id="floor">
<option value="A1">A1</option>
<option value="B1">B1</option>
</select><br />

Javascript:

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>

<script>
$("#garage").change(function () {
    var val = $(this).val();
    if (val == "F") {
        $("#floor").html("<option value='A1'>A1</option><option value='B1'>B1</option>");
    } else if (val == "K") {
        $("#floor").html("<option value='A2'>A2</option><option value='B2'>B2</option>");
    }
});

</head>

要在您的 PHP 应用程序中实现这一点,只需将上述代码放入<head>您的 html 部分。

于 2013-07-08T21:58:48.973 回答
0

好的,这是解决方案,重新编辑,刚刚测试过:

<script>
function garage_onchange(){
    var selGarageValue = document.getElementById("garage").value;
    var selFloor = document.getElementById("floor");

    for(var i=selFloor.options.length-1;i>=0;i--){
        selFloor.remove(i);
    }

    if(selGarageValue=="F"){
        var option = document.createElement('option');
        option.text = "A1";
        option.value="A1";
        selFloor.add(option);
        option = document.createElement('option');
        option.text = "B1";
        option.value="B1";
        selFloor.add(option);
    }
    else if(selGarageValue=="K"){
        var option = document.createElement('option');
        option.text = "A2";
        option.value="A2";
        selFloor.add(option);
        option = document.createElement('option');
        option.text = "B2";
        option.value="B2";
        selFloor.add(option);
    }
}
</script>

Garage: 
<select id="garage" name="garage" onchange="garage_onchange();">
<option value="S" selected="selected">-Choose Garage-</option>
<option value="F">F</option>
<option value="K">K</option>
</select>

<br />
Floor: 
<select name="floor" id="floor">
</select>
于 2013-07-08T21:38:37.020 回答