javascript 如何通过从另一个下拉列表中选择值来填充下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18526914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:16:34  来源:igfitidea点击:

How can i populate a dropdown list by selecting the value from another dropdown list

phpjavascriptjqueryhtmldrop-down-menu

提问by Ashoka

i have two the 1st one say country

我有两个第一个说国家

country_id          country _name
1                   India
2                   Australia
3                   Netherlands

the 2nd table is states
state_id           country         state name
1                   2              abc
2                   2              xyz
3                   3              pqr
4                   1              lmn
5                   1              rst

where country_idin the first table is the country in the second table So now i want two drop downs one for country and another for states. The Second drop down values must must change as per the selected item in the first drop down and the second drop down must be disabled as long as the items in the first drop down is not selected

其中,country_id在第一个表是在第二个表中的国家,所以现在我想要两个下拉菜单一个国家与另一个国家的状态。第二个下拉值必须根据第一个下拉列表中的选定项目进行更改,并且只要未选择第一个下拉列表中的项目,就必须禁用第二个下拉列表

回答by Saranya Sadhasivam

Do a html as given below for Make

为 Make 执行如下所示的 html

<?php $sql = "SELECT * FROM country";
$result = mysql_query($sql);
?>
<select id="country" name='country' onchange="get_states();">
<option value=''>Select</option>
<?php while ($row = mysql_fetch_array($result)) {
    echo "<option value='" . $row['country_id'] . "'>" . $row['country_name'] . "</option>";}
?>
</select>
<div id="get_state"></div> // Sub will be appended here using ajax

Write a ajax function get_states();

写一个ajax函数get_states();

<script type="text/javascript">
function get_states() { // Call to ajax function
    var country = $('#country').val();
    var dataString = "country="+country;
    $.ajax({
        type: "POST",
        url: "getstates.php", // Name of the php files
        data: dataString,
        success: function(html)
        {
            $("#get_state").html(html);
        }
    });
}
</script>

File getstates.php - Will get sub from the below file which will be appended to the div

文件 getstates.php - 将从下面的文件中获取子文件,该文件将附加到 div

if ($_POST) {
    $country = $_POST['country'];
    if ($country != '') {
       $sql1 = "SELECT * FROM state WHERE country=" . $country;
       $result1 = mysql_query($sql1);
       echo "<select name='state'>";
       echo "<option value=''>Select</option>"; 
       while ($row = mysql_fetch_array($result1)) {
          echo "<option value='" . $row['state_id'] . "'>" . $row['state_name'] . "</option>";}
       echo "</select>";
    }
    else
    {
        echo  '';
    }
}