gpt4 book ai didi

jQuery 组合框/自动完成但可编辑

转载 作者:行者123 更新时间:2023-12-01 01:03:12 25 4
gpt4 key购买 nike

我正在使用 jQuery Autocomplete但我需要它是可编辑的。我的意思是,如果列表中没有某个值,我需要捕获他们输入的值。使用上面链接中的示例,用户可能看不到他们的选择(例如 C#),并且会键入他们的语言。我在默认行为中发现,如果在列表中找不到该值,则会清除用户响应。

我添加了以下代码:

options: {
allowUserDefined: false
},

var self = this,
**options = this.options,**

if (!valid && !options.allowUserDefined) {
// remove invalid value, as it didn't match anything
$(this).val("");
select.val("");
input.data("autocomplete").term = "";
return false;
}

防止响应被清除。这些小小的改变确实有效。但由于列表中未找到 C#,因此没有可返回的选定选项。

如何让代码返回输入字段中输入的内容?在调试代码时,特别是在 change 事件中,input.val() 包含输入的文本,这就是我所需要的。我只是不知道如何访问这个值。

谢谢。

这是我完整的js文件:

(function ($) {
$.widget("ui.combobox", {
options: {
allowUserDefined: false
},
_create: function () {
if (this.options.allowUserDefined === null) {
this.options.allowUserDefined = false;
}

var self = this,
options = this.options,
select = this.element.hide(),
selected = select.children(":selected"),
value = selected.val() ? selected.text() : "";
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 && !options.allowUserDefined) {
// remove invalid value, as it didn't match anything
$(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'>&nbsp;</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 () {
// close if already visible
if (input.autocomplete("widget").is(":visible")) {
input.autocomplete("close");
return;
}

// work around a bug (likely same cause as #5265)
$(this).blur();

// pass empty string as value to search for, displaying all results
input.autocomplete("search", "");
input.focus();
});
},

destroy: function () {
this.input.remove();
this.button.remove();
this.element.show();
$.Widget.prototype.destroy.call(this);
}
});
})(jQuery);

更新

使用 Darkajax 的代码(我比我想出的更喜欢严格选项),我可以通过这样做来获取输入框的值:

var valu = $("#combobox").parent().children()[1].value;

最佳答案

当使用 jQuery UI 自动完成组合框时,我自己所做的是声明组合框小部件,如下所示:

(function( $ ) {
$.widget( "ui.combobox", {
// default options
options: {
strict: false
},
_create: function() {
var self = this,
select = this.element.hide(),
selected = select.children( ":selected" ),
value = selected.val() ? selected.text() : "",
strict = this.options.strict;

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
});
},
autocomplete : function(value) {
this.element.val(value);
this.input.val(value);
},
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.value.match( matcher ) ) {
this.selected = valid = true;
return false;
}
});
if ( !valid ) {
// if strict is true, then unmatched values are not allowed
if ( strict ) {
// remove invalid value, as it didn't match anything
$( this ).val( "" );
select.val( "" );
}
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\" class=combo_button>&nbsp;</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() {
// close if already visible
if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
input.autocomplete( "close" );
return;
}

// pass empty string as value to search for, displaying all results
input.autocomplete( "search", "" );
input.focus();
});
}
});
})( jQuery );

这将使它接受其他选项作为有效值,您只需使用:

$("#combobox").combobox({
strict: true
});

使其默认工作,以防万一您需要它。

关于jQuery 组合框/自动完成但可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9473268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com