javascript - 基本动态选择列表 PHP 和 AJAX Jquery

标签 javascript php jquery mysql ajax

我试图使用 AJAX 获取动态依赖选择列表,但无法获取第二个列表。这是我的代码。 gethint.php 工作正常。我不知道我哪里做错了。

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"</script>      

        <script>
        $(document).ready(function()
        {
            $('#brand').change(function()
            {
                var cid=$('#brand').val();
                if(cid !=0)
                {

                $.ajax({
                    type:'post',
                    url: 'gethint.php',
                    data: {id:cid},
                    cache:false,
                    success: function(returndata)
                        {
                            $('#model').html(returndata);
                        }
                    });
                }
            })
        })
        </script>
    </head>
    <body>
        <header>
            <h1>Car Comparision </h1>
        </header>
        <form method="post" action="">
            Brand 1:
            <select id="brand" class="brand">
                    <?php
                    include "connect.php"; 
                    $query=$con->query("SELECT * FROM car");
                    while($brand=$query->fetch_assoc())
                    {
                    $brand_sel='<option value="'.$brand['id'].'"'.">".$brand['brand'].'</option>'."\n";
                    echo $brand_sel;
                    }
                    ?>
            </select>
            Model 1:
            <select id="model" class="model">
            <option value="0">Please select a city</option>
                <option></option>
            </select>
            <input type="submit" value="submit">
        </form>
    </body>
</html>

我的 gethint.php 文件的代码

<?php
require ("connect.php");
$Query='SELECT * FROM model WHERE id='.$_POST['id'];
$sql=$con->query($Query) or die(mysql_error());
//print_r($Query);
while($row=$sql->fetch_array(MYSQLI_ASSOC)) {
    ?>
    <option value="<?php echo $row["id"];?>"><?php echo $row['model_name'];?></option>
<?php
}
?>

最佳答案

请试试这段代码

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

    <script>
    $(document).ready(function()
    {
       $('#brand').on('change', function() {
            var cid=$('#brand').val();
            if(cid !=0)
            {

            $.ajax({
                type:'post',
                url: 'gethint.php',
                data: {id:cid},
                cache:false,
                success: function(returndata)
                    {
                        $('#model').html(returndata);
                    }
                });
            }
        })
    })
    </script>

关于javascript - 基本动态选择列表 PHP 和 AJAX Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32740145/

相关文章:

javascript - IE中有AJAX进度事件吗?如何使用?

php - 我可以在 SQL 查询中添加一些 PHP 代码吗?

php - Javascript 警报框和确认后

javascript - 让 animatescroll.js 在 Phonegap/Cordova/AppBuilder 中工作

jquery - 为什么 $(this).animate 不起作用

javascript - Angular : Mixing provider and custom service in module's config/run

c# - 创建谷歌图表并转换为图像服务器端

php - 使用 PHP 实现双向加密的最佳方法是什么?

jquery - 在 GAE 中找不到背景图像

javascript - 如何修复屏幕底部的页脚?