从optgroup中选择单个项目

时间:2015-05-04 06:24:32

标签: jquery twitter-bootstrap select optgroup

我想从optgroup中选择4项来自select的单项。 我尝试了由silviomoreto bootstrap-silviomoreto引导选择,但它没有按预期工作...... 我只是想用jquery来做这个,不一定是上面的插件..



<select class="selectpicker" multiple data-max-options="4">
    <optgroup label="Group1" data-max-options="1">
      <option>Subject-1</option>
      <option>Subject-2</option>
      <option>Subject-3</option>
    </optgroup>
    <optgroup label="Group2" data-max-options="1">
      <option>Subject-1</option>
      <option>Subject-2</option>
      <option>Subject-3</option>
    </optgroup>
	<optgroup label="Group3" data-max-options="1">
      <option>Subject-1</option>
      <option>Subject-2</option>
     </optgroup>
	<optgroup label="Group5" data-max-options="1">
      <option>Subject-1</option>
      <option>Subject-2</option>
     </optgroup>
	<optgroup label="Group6" data-max-options="1">
     <option>Subject-1</option>
      <option>Subject-2</option>
     </optgroup>
  </select>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您可以尝试这样:

$("option").on("click", function(){
    if($(this).siblings("option:selected").length) {
       $(this).siblings("option").attr("selected",false);
    }
    if($(this).closest("select").find("option:selected").length>4) {
       $(this).attr("selected",false);    
    }
});

FIDDLE