我正在将 jquery 与 dropdownchecklist 项目一起使用。 这是我配置选择框的代码。
jQuery('#selectbox').dropdownchecklist({ width: 120, maxDropHeight: 100, firstItemChecksAll: false, emptyText: 'Select' });
我想将选择限制为仅 2 个选择。 如果用户选择 2 个选项,则所有其他项目将无法选择。 我该怎么做?
更新: 我找到了最简单的方法来做到这一点
function Selectbox_limit(jidList) {
var jids = '';
var counter = 0;
for(var i=0; i<jidList.options.length; i++) {
if(jidList.options[i].selected == true)
counter++;
if(counter >= 2) {
jidList.options[i-1].selected = false;
jQuery("#selectbox").dropdownchecklist("destroy");
jQuery("#selectbox option").attr('disabled','disabled');
jQuery("#selectbox option:selected").attr("disabled","");
jQuery('#selectbox').dropdownchecklist({ _propeties_ });
return;
} else if(counter < 2) {
jQuery("#selectbox").dropdownchecklist("destroy");
jQuery("#selectbox option").attr("disabled","");
jQuery('#selectbox').dropdownchecklist({ _propeties_ });
return;
}
}
最佳答案
下拉列表将事件类添加到复选框中,以便您可以像这样使用它:
$('.active').change(function () {
var num = 0;
$('.active:checked').each(function () {
num++;
});
if(num >= 2)
{
$('.active:checked').attr("disabled", "disabled");
$('.active:checked').each(function () {
$(this).removeAttr();
});
}
else
{
$('.active').removeAttr("disabled", "disabled");
}
});
关于javascript - jQuery限制 "Dropdown Check List"选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9581198/