2014-01-21 31 views
0

我使用了2個jQuery UI小部件來添加選項,只允許用戶在兩​​個下拉小部件之間選擇5個總數複選框。我可以阻止用戶在5次擊中後檢查更多的盒子,但無法取消選中。下面的代碼不會有任何限制。任何幫助,將不勝感激。我使用JS - 添加2個多重選擇複選框

UI部件:http://www.erichynds.com/blog/jquery-ui-multiselect-widget

<select id="dropdown1" multiple="multiple" class="multiselect"> 
<select id="dropdown2" multiple="multiple" class="multiselect"> 

JS

$(document).ready(function() { 
    $(".multiselect").multiselect({ 
     header: "Choose up to 5 areas total", 
     click: function (event, ui) { 
      if (!this.checked) return true; 

      if ($(".multiselect").children(":checked").length >= 5) { 
       return false; 
      } 
     }, 
     selectedList:5 
    }); 
}); 

除去return true IF允許用戶僅選擇5這也防止用戶從取消選中。

+0

的可能重複[jQuery UI的多選控件 - 無法取消選中複選框(http://stackoverflow.com/questions/21194696/jquery-ui-multiselect-widget-cant-uncheck-checkbox) –

回答

1

嘗試

$(document).ready(function() { 
    $(".multiselect").multiselect({ 
     header: "Choose up to 5 areas total", 
     click: function (event, ui) { 

      if (!this.checked && $(".multiselect").children(":checked").length >= 5) { 
       return false; 
      } 
     }, 
     selectedList:5 
    }); 
}); 

這樣,如果用戶點擊一個非檢查框條件return false只會得到滿足。如果用戶點擊複選框,它將允許取消選中它。

+0

仍無法取消滿足5後。 – user3191137

+0

你可以製作一個JSFiddle嗎? – marekful

+0

不知道我能夠使用多個來源。 js文件來填充下拉列表等 – user3191137