Javascript Jquery自动完成插件不允许自由用户输入
所以我从jquery获得了这个代码示例。这是一个自动完成功能,仅允许所有WOS用户选择数据库中的值。我想要的是,它还可以从用户那里获取本地输入。你能帮我吗。谢谢Javascript Jquery自动完成插件不允许自由用户输入,javascript,jquery,autocomplete,Javascript,Jquery,Autocomplete,所以我从jquery获得了这个代码示例。这是一个自动完成功能,仅允许所有WOS用户选择数据库中的值。我想要的是,它还可以从用户那里获取本地输入。你能帮我吗。谢谢 <script> (function( $ ) { $.widget( "custom.combobox", { _create: function() { this.wrapper = $( "<span>" )
<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({
tooltipClass: "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;
$( "<a>" )
.attr( "tabIndex", -1 )
.attr( "title", "Show All Items" )
.tooltip()
.appendTo( this.wrapper )
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass( "ui-corner-all" )
.addClass( "custom-combobox-toggle ui-corner-right" )
.mousedown(function() {
wasOpen = input.autocomplete( "widget" ).is( ":visible" );
})
.click(function() {
input.focus();
// Close if already visible
if ( wasOpen ) {
return;
}
// Pass empty string as value to search for, displaying all results
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 ) {
// Selected an item, nothing to do
if ( ui.item ) {
return;
}
// Search for a match (case-insensitive)
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;
}
});
// Found a match, nothing to do
if ( valid ) {
return;
}
// Remove invalid value
this.input
.val( "" )
.attr( "title", value + " didn't match any item" )
.tooltip( "open" );
this.element.val( "" );
this._delay(function() {
this.input.tooltip( "close" ).attr( "title", "" );
}, 2500 );
this.input.data( "ui-autocomplete" ).term = "";
},
_destroy: function() {
this.wrapper.remove();
this.element.show();
}
});
})( jQuery );
$(function() {
$( "#contact" ).combobox();
$( "#cuscode" ).combobox();
$( "#category" ).combobox();
$( "#group" ).combobox();
$( "#subbrand" ).combobox();
$( "#model" ).combobox();
$( "#compmodel" ).combobox();
$( "#toggle" ).click(function() {
$( "#combobox" ).toggle();
});
});</script>
(函数($){
$.widget(“custom.combobox”{
_创建:函数(){
this.wrapper=$(“”)
.addClass(“自定义组合框”)
.insertAfter(此元素);
this.element.hide();
这是。_createAutocomplete();
这是。_createShowAllButton();
},
_createAutocomplete:function(){
var selected=this.element.children(“:selected”),
value=selected.val()?selected.text():“”;
this.input=$(“”)
.appendTo(this.wrapper)
.val(值)
.attr(“标题”、“名称”)
.addClass(“自定义组合框输入ui小部件ui小部件内容ui状态默认ui左角”)
.自动完成({
延迟:0,
最小长度:0,
来源:$.proxy(此“\u来源”)
})
.工具提示({
tooltipClass:“ui状态突出显示”
});
此。_on(this.input{
自动完成选择:功能(事件、用户界面){
ui.item.option.selected=true;
此._触发器(“选择”,事件{
项目:ui.item.option
});
},
AutoCompleteTechange:“\u RemoveFinValid”
});
},
_createShowAllButton:函数(){
var input=this.input,
wasOpen=false;
$( "" )
.attr(“tabIndex”,-1)
.attr(“标题”、“显示所有项目”)
.工具提示()
.appendTo(this.wrapper)
.按钮({
图标:{
主要:“ui-icon-triangle-1-s”
},
文本:false
})
.removeClass(“用户界面角全部”)
.addClass(“自定义组合框切换ui右角”)
.mousedown(函数(){
wasOpen=input.autocomplete(“小部件”)是(“:可见”);
})
。单击(函数(){
input.focus();
//如果已经可见,请关闭
如果(打开){
返回;
}
//将空字符串作为要搜索的值传递,显示所有结果
input.autocomplete(“搜索”和“);
});
},
_来源:功能(请求、响应){
var matcher=newregexp($.ui.autocomplete.escapeRegex(request.term),“i”);
响应(this.element.children(“option”).map(函数(){
var text=$(this.text();
if(this.value&(!request.term | | matcher.test(text)))
返回{
标签:文本,
值:文本,
选项:这个
};
}) );
},
_RemoveFinValid:函数(事件,ui){
//已选择项目,无需执行任何操作
if(ui.item){
返回;
}
//搜索匹配项(不区分大小写)
var value=this.input.val(),
valueLowerCase=value.toLowerCase(),
有效=错误;
this.element.children(“option”).each(function(){
if($(this).text().toLowerCase()==valueLowerCase){
this.selected=valid=true;
返回false;
}
});
//找到一根火柴,没什么可做的
如果(有效){
返回;
}
//删除无效值
这是输入
.val(“”)
.attr(“标题”,值+“不匹配任何项目”)
.工具提示(“打开”);
this.element.val(“”);
这是延迟(函数(){
this.input.tooltip(“close”).attr(“title”,”);
}, 2500 );
此.input.data(“ui自动完成”).term=“”;
},
_销毁:函数(){
this.wrapper.remove();
this.element.show();
}
});
})(jQuery);
$(函数(){
$(“#联系人”).combobox();
$(“#cuscode”).combobox();
$(“#类别”).combobox();
$(“#组”).combobox();
$(“#子品牌”).combobox();
$(“#model”).combobox();
$(“#compmodel”).combobox();
$(“#切换”)。单击(函数(){
$(“#组合框”).toggle();
});
});
只需从代码中删除这一行自动完成更改:“\u removeIfInvalid”
和\u removeIfInvalid=function()
块。使用jQueryUI自动完成模块不是一个选项?@АаааПччаааа抱歉,我没听清你想说的话。我的代码完全来自jQueryUI自动完成模块。它只接受来自数组或数据库的输入。但我也希望用户从combobox中输入一个新值。我尝试删除它,但仍然无法获得combobox的值。它只允许输入,但在传递值后仍为空。然后返回autocompletechange:“\u removeIfInvalid”
,并使\u removeIfInvalid=function(event,ui){return;}