问问题
408 次
1 回答
0
我自己解决了!
我向 bootstrap-select.js 添加了一个名为 selectQuery 的方法
/*!
* bootstrap-select v1.4.1
* http://silviomoreto.github.io/bootstrap-select/
*
* Copyright 2013 bootstrap-select
* Licensed under the MIT license
*/
var Selectpicker = function(element, options, e) {
if (e) {
e.stopPropagation();
e.preventDefault();
}
this.$element = $(element);
this.$newElement = null;
this.$button = null;
this.$menu = null;
//Merge defaults, options and data-attributes to make our options
this.options = $.extend({}, $.fn.selectpicker.defaults, this.$element.data(), typeof options == 'object' && options);
//If we have no title yet, check the attribute 'title' (this is missed by jq as its not a data-attribute
if (this.options.title === null) {
this.options.title = this.$element.attr('title');
}
//Expose public methods
this.val = Selectpicker.prototype.val;
this.render = Selectpicker.prototype.render;
this.refresh = Selectpicker.prototype.refresh;
this.setStyle = Selectpicker.prototype.setStyle;
//this is what I add
this.selectQuery = Selectpicker.prototype.selectQuery;
this.selectAll = Selectpicker.prototype.selectAll;
this.deselectAll = Selectpicker.prototype.deselectAll;
this.init();
};
selectQuery: function(){
var lis = this.$newElement.find('li');
var options = this.$element.find('option');
for(var i = 0 ;i<lis.length;i++){
if(lis.eq(i).css('display') != 'none'){
options.eq(i).prop('selected', true).attr('selected', 'selected');
}
}
this.render();
},
要触发 selectQuery,只需添加一个 btn 并添加点击事件
<button type="button" class="btn btn-success btn-sm" id="selectbtn">全选</button>
<script type="text/javascript">
$(function(){
$('#selectId').selectpicker();
$("#selectbtn").click(function(){
$('#selectId').selectpicker('selectQuery');
});
}
</script>
于 2014-01-20T09:39:21.073 回答