【发布时间】:2014-12-18 22:30:13
【问题描述】:
想扩展此代码,但注意到它无法在较新版本的 jQuery 和 JQM 中限制选择(按计数)。
Here is a fiddle 表明它适用于 jQuery 1.8.3 和 JQM 1.2.0。
jQuery(减去 Select 本身)代码在这里:
$(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 来保存选择,但想在用户需要更新时向他们展示他们已经保存的内容。
【问题讨论】:
标签: javascript jquery ajax jquery-mobile