将multiselect中的一些特定选项设置为禁用和选中 [英] set some specific options in multiselect as disabled and selected
问题描述
我有一个多选框,有一些选项,我可以通过jquery选择或禁用,但我不能让他们同时做两个。我怎么能通过jquery做到这一点?
I have a multiselect box having some options which i am able to select or disable via jquery, but i am not able to make them do both at the same time. How can i do that via jquery ??
这就是我在做什么 - JSFiddle
Here's what i was doing - JSFiddle
在这个例子中,我在加载时选择一个选项,然后禁用它,但该选项仍然是可选择的。我希望一直选择和禁用它。
In this example i am selecting an option on load and after that disabling it, but the option is still de selectable. I want to keep it selected and disabled all the time.
现在我使用点击方法通过添加以下行来保持选择:
For now i am using click method to keep it selected by adding this line:
$('#multiSelect').click(function()
{$('#multiSelectoption[value="three"]').attr('selected',true);});
编辑:
人们发布解决方案,我们再次点击选择该选项,我已经在我的问题中发布了。我想知道的是,如果存在一些更好的解决方案,因为这个看起来不太好。
People are posting the solution where we select the option again on click, which i already have posted in my question. What i wanted to know is that if there exist some better solution as this one does not look good.
推荐答案
js小提琴:
http:// jsfiddle .net / Tf5ZZ / 5 /
jquery:
//disabling on load:
$('#multiSelect option[value="three"]').prop('disabled', true);
$("#multiSelect option").on('click',function() {
$('#multiSelect option[value="three"]').prop('selected',true);
});
这篇关于将multiselect中的一些特定选项设置为禁用和选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!