jQuery限制“下拉列表”选择 [英] jQuery limit "Dropdown Check List" selects
本文介绍了jQuery限制“下拉列表”选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的配置选择框的代码。
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({_propvals_});
return;
} else if(counter< 2){
jQuery(#selectbox)。dropdownchecklist(destroy);
jQuery(#selectbox option)。attr(disabled,);
jQuery('#selectbox')。dropdownchecklist({_propvals_});
return;
}
}
解决方案
dropdownchecklist将激活的类添加到复选框,以便您可以使用它:
$('。active')。 (function(){
var num = 0;
$('。active:checked')。each(function(){
num ++;
});
if(num> = 2)
{
$('。active:checked')。attr(disabled,disabled);
$(' ').each(function(){
$(this).removeAttr();
});
}
else
{
$ '.active')。removeAttr(disabled,disabled);
}
});
I'm using jquery with dropdownchecklist item. this is my code to config selectbox.
jQuery('#selectbox').dropdownchecklist({ width: 120, maxDropHeight: 100, firstItemChecksAll: false, emptyText: 'Select' });
I want to limit the select for only 2 selects. If the user select 2 options all others item will disable for selecting. How can I do it?
Update: I found the easiest way to do this
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;
}
}
解决方案
The dropdownchecklist add the class active to the check boxes so you can use it like this:
$('.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");
}
});
这篇关于jQuery限制“下拉列表”选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文