如何在Javascript/HTML中创建级联下拉列表

How do you create cascading drop-down lists in Javascript/HTML?

本文关键字:创建 级联 下拉列表 HTML Javascript      更新时间:2023-09-26

>假设我从下拉列表中选择一个项目,然后根据所选项目,我将如何显示另一个下拉列表?例如,在下面的代码中,如果我选择选项"印度",我想显示另一个包含 2-3 个州名称的下拉框。

<! DOCTYPE html>
<html>
<head>
<script type="text/javascript">
    function india()
    {
        var x =document.getElementById('con').value;
        alert(x);
    }
</script>
</head>
<body>
    <h1 id='h1'> first </h1>
    <select id="con" onchange="indai()"> 
        <option>----select----</option>
        <option value="india" >India</option>
        <option value="US">US</option>
        <option value="UK">UK</option>
    </select>
</body>
</html>

http://jsfiddle.net/7FtkS/

这是一个很好的解决方案。

onchange="document[value](value)"
// where the [value] defines the name of the function and (value) used as id-parm
document.india = function(id)
{
    document.getElementById(id).style.display='';
}