您可以通过稍作更改(注意 - 它可能会导致副作用)来调整引导开关插件(我使用 v3.3.2 ):
从:
$.fn.bootstrapSwitch = function() {
var args, option, ret;
option = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
ret = this;
this.each(function() {
var $this, data;
$this = $(this);
data = $this.data("bootstrap-switch");
if (!data) {
$this.data("bootstrap-switch", data = new BootstrapSwitch(this, option));
}
if (typeof option === "string") {
return ret = data[option].apply(data, args);
}
});
return ret;
};
至:
$.fn.bootstrapSwitch = function() {
var args, option, ret;
option = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
ret = this;
this.each(function() {
var $this, data;
$this = $(this);
data = $this.data("bootstrap-switch");
if (!data) {
$this.data("bootstrap-switch", data = new BootstrapSwitch(this, option));
}
if (typeof option === "string") {
//this section was modified (start)!
var temp = data[option].apply(data, args);
return ret = option == 'state' ? data[temp ? 'onText' : 'offText'].apply(data, args) : temp;
//this section was modified (end)!
}
});
return ret;
};
执行:
var res = $('#mycheckbox').bootstrapSwitch('state');
//res == 'Enable' or 'Disable'
PS如果您只使用一次,您可以简单地执行此操作(无需对插件进行任何更改):
var element = $('#mycheckbox');
var res = element.bootstrapSwitch(element.bootstrapSwitch('state') ? 'onText' : 'offText');