简体   繁体   English

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

[英]Creating two drop down list that are dependent on the first

I am creating three drop down lists with all the possible options in my html. 我正在用HTML中的所有可能选项创建三个下拉列表。 However, I need for them to change as the previous options are picked (hide and display the right ones). 但是,我需要随着先前选项的选择(隐藏并显示正确的选项)进行更改。 I got the second one to tie in to the first, I can't tie the third one in. How do I do this with this same style of code? 我有第二个要绑定到第一个,我不能绑定第三个。我如何用相同的代码风格来做到这一点?

http://jsfiddle.net/cL2tt/115/ 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();
})

See my answer to a similar question, showing 2 different ways. 看到我对类似问题的回答,显示了两种不同的方式。

Changing the values in a <select> using another <select> using AJAX (PHP) 使用AJAX(PHP)使用另一个<select>更改<select>中的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM