【发布时间】:2015-07-13 11:34:12
【问题描述】:
我正在使用此处接受的答案中的代码
How do you limit options selected in a html select box?
计算“选择多个”菜单中的选定选项:
var last_valid_selection = null;
$("#select_options").change(function(event) {
if ($(this).val().length > 10) {
$(this).val(last_valid_selection);
} else {
last_valid_selection = $(this).val();
$("#select_options_text").text("Please select at least one, and up to ten options. You have currently selected "+$(this).val().length);
}
});
菜单分为六个 optgroup。当我点击 10 个选项时,我无法再按预期进行选择。但是我也不能再使用 CTRL+单击选定的选项来取消选择它们。
如果我删除所有 optgroup,菜单将正常运行。它还可以与一个和两个 optgroup 一起正常工作。似乎只有在添加第三个 optgroup 时才会出现上述问题。
我在 Chrome 和 Firefox 中进行了测试,两者都出现了问题。
【问题讨论】:
-
你能在 jsfiddl 中添加你的 HTML,甚至更好地重现它
-
在 jsFiddle 上重现问题,在此处按预期工作:jsfiddle.net/z9r26r3j/1
-
这是一个使用实际选择菜单选项的 jsFiddle:jsfiddle.net/gzdrL5wu/2
标签: javascript jquery select optgroup