创建两个依赖于第一个的下拉列表

Creating two drop down list that are dependent on the first

本文关键字:依赖于 第一个 下拉列表 两个 创建      更新时间:2023-09-26

我在我的html中创建了三个下拉列表,其中包含所有可能的选项。然而,我需要他们改变,因为以前的选项被选中(隐藏和显示正确的)。我把第二个和第一个绑在一起了,我不能把第三个绑在一起了。我如何用同样风格的代码做到这一点?

http://jsfiddle.net/cL2tt/115/

$(document).ready(function() {
    var optarray = $("#optthree").children('option').map(function() {
        return {
            "value": this.value,
            "option": "<option value='" + this.value + "'>" + this.text + "</option>"
        }
    })
    $("#opttwo").change(function() {
        $("#optthree").children('option').remove();
        var addoptarr = [];
        for (i = 0; i < optarray.length; i++) {
            if (optarray[i].value.indexOf($(this).val()) > -1) {
                addoptarr.push(optarray[i].option);
            }
        }
        $("#optthree").html(addoptarr.join(''))
    }).change();
})

看我对类似问题的回答,展示了两种不同的方式。

修改使用AJAX (PHP)