Bootstrap Selectpicker

时间:2014-02-25 05:28:22

标签: twitter-bootstrap

我有一个selectpicker,它可以动态构建下拉值,并将'All'作为第一个选项。单击“全部”时我需要选择全部/无,并且当选择/取消选择剩余任何选项时,需要选中/取消选中“全部”。能够使用以下代码获取Select All / None。当选中/取消选中剩余选项时,需要帮助取消选中/选中“全部”。

$('.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;
});

1 个答案:

答案 0 :(得分: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;