2014-02-25 67 views
0

我有一個selectpicker,它動態地構建下拉值,並將'All'作爲第一個選項。當點擊「全部」時,我需要選擇全部/無,當剩餘的選項被選中/未選中時,需要選中/取消選中「全部」。能夠通過下面的代碼獲得全選/無選擇。當剩餘選項被選中/取消選中時,需要幫助取消選中/選中「全部」。Bootstrap Selectpicker

$('.selectallselstatus').click(function() { 
    var firstli = $('a.selectallselstatus).parent(); 
    if (!firstli.hasClass('selected')) { 
     $('.selectpicker.selstatusall').selectpicker('selectAll'); 
     $("button[data-id='selstatus']").attr('title', "All"); 
     $("button[data-id='selstatus']").find('> span[class="filter-option pull-left"]').text($("button[data-id='selstatus']").attr('title')); 
    } else { 
     $('.selectpicker.selstatusall').selectpicker('deselectAll'); 
    } 
    return false; 
}); 

回答

0

通過使用這段代碼解決。希望有助於他人。

var firstli = $('a.selectallselstatus').parent(); 
firstli.removeClass('selected'); 
var a = $('#selstatus').val(); 
a = a == null ? "" : a.toString().replace("-1,", ""); 
if((total elements in the dropdown) == a.split(",").length) { 
    firstli.addClass('selected'); 
    $("button[data-id='selstatus']").attr('title', "All"); 
    $("button[data-id='selstatus']").find('> span[class="filter-option pull-left"]').text($("button[data-id='selstatus']").attr('title')); 
} else {   
    var title = $("button[data-id='selstatus']").attr('title'); 
    if(title.indexOf("All,") > -1) title = title.replace("All, ", ""); 
    $("button[data-id='selstatus']").attr('title', title); 
    $("button[data-id='selstatus']").find('> span[class="filter-option pull-left"]').text(title); 
} 
return false; 
相關問題