为什么这个选择菜单在jQuery和JQM的新版本组合中失败

Why does this select menu fail in new version combo of jQuery and JQM?

本文关键字:JQM 新版本 组合 失败 jQuery 选择 菜单 为什么      更新时间:2023-09-26
想要

扩展此代码,但注意到它无法限制较新版本的jQuery和JQM中的选择(按计数(。

这是一个小提琴,表明它适用于jQuery 1.8.3和JQM 1.2.0。

jQuery(减去选择本身(代码在这里:

$(document).on("pageinit", function () {
    $("#select").on("change", function () {
        var selected = $("option:selected", this).length;
        if (selected == 4) {
            $("option:not(:selected)", this).prop("disabled", true);
            $(this).selectmenu("refresh");
        }
        if (selected < 4) {
            $("option:disabled", this).prop("disabled", false);
            $(this).selectmenu("refresh");
        }
         arr = new Array();
                    $(this).find("option:selected").each(function(index, item) {
                        arr.push($(item).val());
                        console.log(arr);
                    });
    });
});

您可以通过切换到 jQuery 1.11.0 和 JQM 1.4.4 来模拟小提琴中的故障。

然后,选择应该阻止但允许的第 5 个选项。

我错过了什么会导致新版本失败?

PS:任何指向使用 $.ajax GET 填充选择框的最佳方法的指针也值得赞赏,因为这将是我的下一步。我正在使用 POST 来保存选择,但希望在需要更新时向用户显示他们已经保存的内容。

您必须将true作为第二个参数传递给selectmenu函数,以强制它在禁用该选项后刷新菜单。检查这个小提琴

$(this).selectmenu("refresh", true);