大家好,我一直在使用 jquery 自动完成组合框,一切正常,直到我必须以编程方式更改下拉列表的索引。
看来我必须修改这个
select: function (event, ui) {
ui.item.option.selected = true;
self._trigger("selected", event, {
item: ui.item.option
});
允许我通过代码更新索引,但我不确定如何做到这一点,有人可以帮助我如何完成它。非常感激任何的帮助。组合框代码显示在这里。http://jqueryui.com/autocomplete/#combobox 提前感谢你们可以提供的任何帮助。
我尝试了几种不同的方法,只有当我回到非 jquery 解决方案时它才有效。
$("#StartLocations").val( $("#EndLocations option:selected").val());
$("#EndLocations").prop("selectedIndex", 1);
$("#EndLocations").val(1);
:更新
function RunDirBtn_onclick(r){
if (r == 'Y') {
index = EndLocations.selectedIndex;
StartNode = MystartingLocation[index].locNode;
index = StartLocations.selectedIndex;
EndNode = MystartingLocation[index].locNode;
// $("#StartLocations").val( $("#EndLocations option:selected").val());
// $("#EndLocations").prop("selectedIndex", 1);
// $("#EndLocations").val(1);
var startint = $("#StartLocations").attr("selectedIndex");
var endint = $("#EndLocations").attr("selectedIndex");
$("#EndLocations").attr("selectedIndex", startint);
$("#StartLocations").attr("selectedIndex", endint);
} else {
index = EndLocations.selectedIndex;
EndNode = MystartingLocation[index].locNode;
index = StartLocations.selectedIndex;
StartNode = MystartingLocation[index].locNode;
}
我可以更改每个 dropdpown 的索引我唯一的问题是我不能将它与自动完成组合框一起使用。组合框代码
$.widget("ui.combobox", {
_create: function () {
var self = this,
select = this.element.hide(),
selected = select.children(":selected"),
value = selected.val() ? selected.text() : "Select One...";
var input = this.input = $("<input>")
.insertAfter(select)
.val(value)
.autocomplete({
delay: 0,
minLength: 0,
source: function (request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
response(select.children("option").map(function () {
var text = $(this).text();
if (this.value && (!request.term || matcher.test(text)))
return {
label: text.replace(
new RegExp(
"(?![^&;]+;)(?!<[^<>]*)(" +
$.ui.autocomplete.escapeRegex(request.term) +
")(?![^<>]*>)(?![^&;]+;)", "gi"
), "<strong>$1</strong>"),
value: text,
option: this
};
}));
},
select: function (event, ui) {
ui.item.option.selected = true;
self._trigger("selected", event, {
item: ui.item.option
});
},
change: function (event, ui) {
if (!ui.item) {
var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i"),
valid = false;
select.children("option").each(function () {
if ($(this).text().match(matcher)) {
this.selected = valid = true;
return false;
}
});
if (!valid) {
$(this).val("");
select.val("");
input.data("autocomplete").term = "";
return false;
}
}
}
})
.addClass("ui-widget ui-widget-content ui-corner-left");
input.data("autocomplete")._renderItem = function (ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + item.label + "</a>")
.appendTo(ul);
};
this.button = $("<button type='button'> </button>")
.attr("tabIndex", -1)
.attr("title", "Show All Items")
.insertAfter(input)
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass("ui-corner-all")
.addClass("ui-corner-right ui-button-icon")
.click(function () {
if (input.autocomplete("widget").is(":visible")) {
input.autocomplete("close");
return;
}
$(this).blur();
input.autocomplete("search", "");
input.focus();
});
},
destroy: function () {
this.input.remove();
this.button.remove();
this.element.show();
$.Widget.prototype.destroy.call(this);
}
});