使用jquery过滤每个先前下拉列表选择的下拉列表的结果

时间:2014-02-17 08:38:36

标签: javascript jquery drop-down-menu selectlist

我成功使用了此处发布的jquery脚本TheSuperTramp:

Jquery dependent drop down boxes populate- how

删除任何值小于所选列表项的列表项。但是,我只需要删除我在第一个下拉菜单中选择的值。我相信下面的jquery脚本应该完成这个,但事实并非如此。任何纠正这一点的建议都将非常感激。

谢谢, KS

var drop2 = $("select[id=dropdown] option"); // the collection of initial options
$("select[id=test]").change(function () {
    var drop1selected = parseInt(this.value); //get drop1 's selected value
    $("select[id=dropdown]")
                     .html(drop2) //reset dropdown list
                     .find('option').filter(function () {
                         if (parseInt(this.value) == drop1selected)
                         {
                             $(this).remove();
                         };
                     });
});

1 个答案:

答案 0 :(得分:2)

这里你真正需要的是.each(),而不是.filter()

var drop2 = $("select[id=dropdown] option"); // the collection of initial options
$("select[id=test]").change(function () {
    var drop1selected = parseInt(this.value); //get drop1 's selected value
    $("select[id=dropdown]")
                     .html(drop2) //reset dropdown list
                     .find('option').each(function () {
                         if (parseInt(this.value) === drop1selected)
                         {
                             $(this).remove();
                         };
                     });
});

由于.filter()将从匹配元素的结果集中删除元素,但它不会从DOM中删除它们。您可能希望像这样使用它:

var drop2 = $("select[id=dropdown] option"); // the collection of initial options
$("select[id=test]").change(function () {
    var drop1selected = parseInt(this.value); //get drop1 's selected value
    $("select[id=dropdown]")
                     .html(drop2) //reset dropdown list
                     .find('option').filter(function () {
                         return parseInt(this.value) === drop1selected;
                     }).remove();
});
相关问题