jQuery UI Combobox 是一个基于 jQuery UI 的自定义下拉框控件,它结合了输入框和下拉列表的功能,提供了比原生 <select>
更丰富的交互体验。
有几种方法可以禁用 jQuery UI Combobox:
// 禁用 Combobox
$("#yourCombobox").combobox("option", "disabled", true);
// 启用 Combobox
$("#yourCombobox").combobox("option", "disabled", false);
// 禁用
$("#yourCombobox").prop("disabled", true).combobox("refresh");
// 启用
$("#yourCombobox").prop("disabled", false).combobox("refresh");
// 禁用
$("#yourCombobox").addClass("ui-state-disabled");
// 启用
$("#yourCombobox").removeClass("ui-state-disabled");
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<style>
.custom-combobox {
position: relative;
display: inline-block;
}
.custom-combobox-toggle {
position: absolute;
top: 0;
bottom: 0;
margin-left: -1px;
padding: 0;
}
.custom-combobox-input {
margin: 0;
padding: 0.3em;
}
.ui-state-disabled {
opacity: 0.6;
cursor: not-allowed;
}
</style>
<script>
$(function() {
$.widget("custom.combobox", {
_create: function() {
this.wrapper = $("<span>")
.addClass("custom-combobox")
.insertAfter(this.element);
this.element.hide();
this._createAutocomplete();
this._createShowAllButton();
},
_createAutocomplete: function() {
var selected = this.element.children(":selected"),
value = selected.val() ? selected.text() : "";
this.input = $("<input>")
.appendTo(this.wrapper)
.val(value)
.attr("title", "")
.addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left")
.autocomplete({
delay: 0,
minLength: 0,
source: $.proxy(this, "_source")
})
.tooltip({
classes: {
"ui-tooltip": "ui-state-highlight"
}
});
this._on(this.input, {
autocompleteselect: function(event, ui) {
ui.item.option.selected = true;
this._trigger("select", event, {
item: ui.item.option
});
},
autocompletechange: "_removeIfInvalid"
});
},
_createShowAllButton: function() {
var input = this.input,
wasOpen = false;
$("<button>")
.attr("tabIndex", -1)
.attr("title", "Show All Items")
.appendTo(this.wrapper)
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass("ui-corner-all")
.addClass("custom-combobox-toggle ui-corner-right")
.on("mousedown", function() {
wasOpen = input.autocomplete("widget").is(":visible");
})
.on("click", function() {
input.trigger("focus");
if (wasOpen) {
return;
}
input.autocomplete("search", "");
});
},
_source: function(request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
response(this.element.children("option").map(function() {
var text = $(this).text();
if (this.value && (!request.term || matcher.test(text)))
return {
label: text,
value: text,
option: this
};
}));
},
_removeIfInvalid: function(event, ui) {
if (ui.item) {
return;
}
var value = this.input.val(),
valueLowerCase = value.toLowerCase(),
valid = false;
this.element.children("option").each(function() {
if ($(this).text().toLowerCase() === valueLowerCase) {
this.selected = valid = true;
return false;
}
});
if (valid) {
return;
}
this.input.val("");
this.element.val("");
this._trigger("change", event, {
item: null
});
},
_destroy: function() {
this.wrapper.remove();
this.element.show();
}
});
$("#combobox").combobox();
// 禁用按钮
$("#disableBtn").click(function() {
$("#combobox").combobox("option", "disabled", true);
});
// 启用按钮
$("#enableBtn").click(function() {
$("#combobox").combobox("option", "disabled", false);
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label>Your preferred programming language: </label>
<select id="combobox">
<option value="">Select one...</option>
<option value="JavaScript">JavaScript</option>
<option value="Python">Python</option>
<option value="Java">Java</option>
<option value="C#">C#</option>
<option value="PHP">PHP</option>
</select>
<button id="disableBtn">禁用 Combobox</button>
<button id="enableBtn">启用 Combobox</button>
</div>
</body>
</html>
destroy
方法:destroy
方法:以上方法适用于大多数 jQuery UI Combobox 实现,具体实现可能因自定义代码略有不同。
没有搜到相关的文章