我一直没有成功尝试使多选功能正常运行。我可以像下面这样包装multiSelect调用,它可以工作,但是由于某种原因,如果不像直接的示例代码那样包装,它就行不通。
(function ($) {
$(function () {
$('#mySelectList').multiSelect();
})
})(jQuery);
如果我可以打电话给$('#keep-order').multiSelect('select', 'whatIwant');
但是,如果我理解正确的话,由于范围的原因,调用multiselect select在其他地方不起作用。
如果有帮助,这就是我现在的意思。需要说明的是,除了标有“不起作用”的位之外,以下内容都将起作用。
以下是html加载脚本:
<link type="text/css" href="~/css/multiselect/multiselect.css" rel="stylesheet" />
<link type="text/css" href="~/css/bootstrap.min.css" rel="stylesheet" />
<script type="text/javascript" src="~/Scripts/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="~/js/quicksearch/jquery.quicksearch.js"></script>
<script type="text/javascript" src="~/js/bootstrap/bootstrap.min.js"></script>
<script type="text/javascript" src="~/js/multiselect/multiselect.js"></script>
<script type="text/javascript" src="~/Scripts/myjavascript.js"></script>
myjavascript.js的内容
// This is a combination of quicksearch and multiselect.
; (function ($) {
$(function () {
$('#mySelectList').multiSelect({
keepOrder: true,
selectableHeader: "<div class='searchTitle'>Select: Sap</div><input type='text' id='searchSelectable' class='search-input' autocomplete='off' placeholder='Search: Sap'>",
selectionHeader: "<div class='searchTitle'>Selection:</div><input type='text' class='search-input' autocomplete='off' placeholder='Search: Selection'>",
afterInit: function (ms) {
var that = this,
$selectableSearch = that.$selectableUl.prev(),
$selectionSearch = that.$selectionUl.prev(),
selectableSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selectable:not(.ms-selected)',
selectionSearchString = '#' + that.$container.attr('id') + ' .ms-elem-selection.ms-selected';
that.qs1 = $selectableSearch.quicksearch(selectableSearchString)
.on('keyup', function (e) {
if (e.which === 40) {
that.$selectableUl.focus();
return false;
} else if (e.which === 86 && e.ctrlKey) { // Ctrl + V
// ############################################ //
// ######## The following does not work ######## //
// ############################################ //
$('#mySelectList').multiSelect('select', 'something');
});
$('#searchSelectable').val('');
return false;
}
});
that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
.on('keydown', function (e) {
if (e.which === 40) {
that.$selectionUl.focus();
return false;
}
});
},
afterSelect: function () {
this.qs1.cache();
this.qs2.cache();
},
afterDeselect: function () {
this.qs1.cache();
this.qs2.cache();
}
});
// Hide everything until multiselect does its thing
$("#hideReady").show();
})
})(jQuery);