获取选择选项的值然后输出另一个选择选项

时间:2012-11-15 14:55:21

标签: javascript ajax

我是AJAX和PHP的新手,我有这段代码:

<head>
    <script type="text/javascript">
        function crimeselect(){
            var select = document.getElementById("crime").value;
        }
    </script>
</head>
<body>
    <select name="crime" id="crime" onChange="crimeselect();">
        <option value="CVPerson">Crimes VS Person</option>
        <option value="CVMO">Crimes VS Moral and Order</option>
        </select>

    <select id="CVPerson" onchange="">
        <option>Homicide</option>
        <option>Kidnapping</option>
    </select>

    <select id="CVMO" onchange="">
        <option>Alarm and Scandal</option>
        <option>Assault/Resistance to Authority</option>
    </select>
</body>

我想要的是,当我选择“犯罪VS人”时,id为“CVPerson”的select选项只会出现,而且不会出现id为“CVHO”的select选项。如果我选择“犯罪VS道德和秩序”也是一样。

我不知道怎么做。请给我任何提示。

1 个答案:

答案 0 :(得分:0)

<head>
    <script type="text/javascript">
        function crimeselect(){

            document.getElementById(document.getElementById("crime").value).style.visibility = 'visible';         

        }
    </script>
</head>
<body>
    <select name="crime" id="crime" onChange="crimeselect();">
        <option value="CVPerson">Crimes VS Person</option>
        <option value="CVMO">Crimes VS Moral and Order</option>
        </select>

    <select id="CVPerson" onchange="" style="visibility:hidden;">
        <option>Homicide</option>
        <option>Kidnapping</option>
    </select>

    <select id="CVMO" onchange=""  style="visibility:hidden;">
        <option>Alarm and Scandal</option>
        <option>Assault/Resistance to Authority</option>
    </select>
</body>​​​