【问题标题】:How to cascade jQuery Mobile combo boxes如何级联 jQuery Mobile 组合框
【发布时间】:2015-02-26 06:11:53
【问题描述】:

我开发了一个级联选择菜单,用于更新具有相同值的组合框。

我无法更新多个组合框。例如,我设法将一个父组合框级联到它的 child(a) 组合框。但是我希望 child(b) 等动态更新。

我想到的解决方案是为 3 个组合框创建单独的函数,每当其中任何一个发生更改时,它都会更新。当然,每个父级都有自己的子级(匹配值),因此更改子级组合框不会影响父级。

这是我写的:https://jsfiddle.net/1ospxyer/6/(请在 JSFiddle 的 JQuery mobile 中加载它 - 左侧)

var storeSiteList = $("#select-choice-1>option");
var storeBuildingList = $("#select-choice-2>option");
var storeLocationList = $("#select-choice-3>option");

function siteFilter() {
    $("#select-choice-1").change(function () {
        $("#select-choice-2").append(storeBuildingList);
        var current_site = $(this).val();
        $("#select-choice-2>option").each(function () {
            if (current_site !== $(this).val()) {
                $(this).remove();
                $("#select-choice-2").selectmenu("refresh");
            }
        });

    });
}

function buildingFilter() {
    $("#select-choice-2").change(function () {
        $("#select-choice-3").append(storeLocationList);
        var current_building = $(this).val();
        $("#select-choice-3>option").each(function () {
            if (current_building !== $(this).val()) {
                $(this).remove();
                $("#select-choice-3").selectmenu("refresh");
            }
        });
    });
}

function locationFilter() {
    $("#select-choice-3").change(function () {
        $("#inputFloor").val($(this).val());
    });
}

siteFilter();
buildingFilter();
locationFilter();

有没有办法级联多个组合框(不仅仅是前两个)?

【问题讨论】:

    标签: javascript jquery html jquery-mobile


    【解决方案1】:

    您可以在更新子组合后触发更改事件,以便更新第三个组合。

    $("#select-choice-1").change(function () {
        $("#select-choice-2").append(storeBuildingList);
        var current_site = $(this).val();
        $("#select-choice-2>option").each(function () {
            if (current_site !== $(this).val()) {
                $(this).remove();
                $("#select-choice-2").selectmenu("refresh");
            }
        });
        //trigger change
        $("#select-choice-2").change();
    });
    

    更新FIDDLE

    【讨论】:

    • 非常感谢!我不知道你可以使用这样的更改功能。我一直认为你必须在里面声明一些东西才能让它工作。学到了一些新东西 :) 同样,你能用其他功能做到这一点吗?比如点击,keyup?
    • @Newtonic,它只是 trigger() 的简写:api.jquery.com/trigger。所以,是的,你可以触发任何事件。
    • 这适用于 Android 设备,但只有下一个孩子在 iPhone 上更新。我注意到 iPhone 使用滚动选择,而不是像 Android 这样的可点击选项。你知道有什么方法可以解决这个问题吗?再次感谢。
    • @Newtonic,你是说在 iOS 上不会触发 change 事件吗?
    • 感谢您的回复。我修改了我的代码,并意识到这是导致代码无法正确执行的速度因素。现在它工作正常。再次感谢。
    猜你喜欢
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多