2015-11-26 37 views
1

當我在MaterializeCSS中的多選中取消選擇值時,它仍然在值數組中,我找不到修復它的方法。它的工作原理,如果我不選擇從原來的選項中選擇與一些功能,但$(「下拉含量禮」)。點擊()不會做任何事情,所以我不能只是做類似MaterializeCSS - 多重選擇 - 值在取消選擇後保持在數組中

$('.dropdown-content li.active').click(function() { 
    //take index of this and unselect option with same index from <select> 
}); 

(請忽略的截圖錯誤,它不相關)

enter image description hereenter image description here

回答

4

我有同樣的問題,並寫了一個小的解決方法。 http://jsfiddle.net/9bL25jw9/2/

$(document).ready(function() { 
    $('select').material_select(); 
    $('select').change(function(){ 
     var newValuesArr = [], 
      select = $(this), 
      ul = select.prev(); 
     ul.children('li').toArray().forEach(function (li, i) { 
      if ($(li).hasClass('active')) { 
       newValuesArr.push(select.children('option').toArray()[i].value); 
      } 
     }); 
     select.val(newValuesArr); 
    }); 
});