php - 简单的jquery级联选择

标签 php javascript jquery html mysql

我有这个代码:

<script type="text/javascript">
    $(document).ready(function(){
        $("select#type").attr("disabled","disabled");
        $("select#model").attr("disabled","disabled");
        $("select#category").change(function(){
        $("select#type").attr("disabled","disabled");
        $("select#type").html("<option>Please wait...</option>");
        var id = $("select#category option:selected").attr('value');
        $.post("../select_type.php", {id:id}, function(data){
            $("select#type").removeAttr("disabled");
            $("select#type").html(data);
        });
    });
    $("select#type").change(function(){
        $("select#model").attr("disabled","disabled");
        $("select#model").html("<option>Please wait...</option>");
        var id2 = $("select#type option:selected").attr('value');
        $.post("../select_model.php", {id2:id2}, function(data){
            $("select#model").removeAttr("disabled");
            $("select#model").html(data);
        });
    });
    $("form#select_form").submit(function(){
        var cat = $("select#category option:selected").attr('value');
        var type = $("select#type option:selected").attr('value');
        var model = $("select#model option:selected").attr('value');
        if(cat>0 && type>0 && model >0)
        {
            var model = $("select#model option:selected").html();
            var type = $("select#type option:selected").html();
            $("#result").html('<br>Your choice: ' + type + ' ' + model + '.');
        }
        else
        {
            $("#result").html("<br>You have to fill every field!");
        }
        return false;
    });
});

这是一个三级级联下拉菜单系统,使用PHP从数据库中获取数据。我的问题是,每当我到达第三级(例如大陆 -> 国家 -> 城市),并且我将大陆更改为其他或“未选择”时,其他 2 个级别将不会重置。当大陆选择输入没有选择任何值时,它们应该处于非事件状态并重置。我该如何解决这个问题?

最佳答案

$("select#category").change() 中也禁用/清空 #model,并在执行 ajax 请求和启用 #type 选择之前检查所选值。

关于php - 简单的jquery级联选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18300607/

相关文章:

javascript - JQuery 不加载通过服务获得的效果

jquery - 如果 mailto 或 tel 不存在则隐藏标签

php - 在 PHP 中拆分字符串并获取最后一部分

php - 多个表上的 Codeigniter/PHP 外部 JOIN 语法

php - 当我从选择类型中选择 'any' 选项并单击提交时,我想显示 MySQL 数据库中的所有数据

jquery - 如何多次显示 'JAlert'弹出窗口?

javascript - 在 Javascript 中组合类似的 If 语句

php - 解析错误: syntax error, unexpected T_STRING in … on line 192

javascript - 带标签的 continue 语句; for循环

javascript - 为什么我们不应该在 javascript 中使用++?