Javascript JQuery自动完成组合框在从列表中选择某些值后设置初始空白
我在JQuery autocomplete组合框中加载一个对象列表,当我从列表中选择一个对象时,它会进入所选对象表 我希望在完成后将JQuery组合框设置为第一个值(空)。怎么做 这是combobox的代码。我刚刚添加了对函数addRow()的调用,以便在按enter键或从列表中选择值时自动将行添加到表中:Javascript JQuery自动完成组合框在从列表中选择某些值后设置初始空白,javascript,jquery,jquery-ui,jquery-plugins,Javascript,Jquery,Jquery Ui,Jquery Plugins,我在JQuery autocomplete组合框中加载一个对象列表,当我从列表中选择一个对象时,它会进入所选对象表 我希望在完成后将JQuery组合框设置为第一个值(空)。怎么做 这是combobox的代码。我刚刚添加了对函数addRow()的调用,以便在按enter键或从列表中选择值时自动将行添加到表中: (function( $ ) { $.widget( "ui.combobox", { _create: function() {
(function( $ ) {
$.widget( "ui.combobox", {
_create: function() {
var self = this,
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;
addRow();
self._trigger( "selected", event, {
item: ui.item.option
});
//cambiaPrestazione(ui.item.option.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 ).text().match( matcher ) ) {
this.selected = valid = true;
return false;
}
});
if ( !valid ) {
// 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'> </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 );
$(function() {
// $( "#styleIdPrestazione" ).val("-1");
$( "#styleIdPrestazione" ).combobox();
$( "#toggle" ).click(function() {
$( "#styleIdPrestazione" ).toggle();
});
});
});
(函数($){
$.widget(“ui.combobox”{
_创建:函数(){
var self=这个,
select=this.element.hide(),
selected=select.children(“:selected”),
value=selected.val()?selected.text():“”;
var input=this.input=$(“”)
.insertAfter(选择)
.val(值)
.自动完成({
延迟:0,
最小长度:0,
来源:功能(请求、响应){
var matcher=newregexp($.ui.autocomplete.escapeRegex(request.term),“i”);
响应(select.children(“option”).map(函数(){
var text=$(this.text();
if(this.value&(!request.term | | matcher.test(text)))
返回{
标签:text.replace(
新正则表达式(
(?![^&;]+;)(?!)(?![^&;]+;),“gi”
)“$1”,
值:文本,
选项:这个
};
}) );
},
选择:功能(事件、用户界面){
ui.item.option.selected=true;
addRow();
自触发(“选定”,事件{
项目:ui.item.option
});
//cambiaPrestazione(ui.item.option.value);
},
更改:功能(事件、用户界面){
如果(!ui.item){
var matcher=new RegExp(“^”+$.ui.autocomplete.escapeRegex($(this.val())+“$”,“i”),
有效=错误;
select.children(“选项”).each(函数(){
if($(this).text().match(matcher)){
this.selected=valid=true;
返回false;
}
});
如果(!有效){
//删除无效值,因为它与任何内容都不匹配
$(此).val(“”);
选择.val(“”);
输入数据(“自动完成”)。术语=”;
返回false;
}
}
}
})
.addClass(“ui小部件ui小部件内容ui左角”);
输入数据(“自动完成”)。\u renderItem=功能(ul,项目){
返回$(“”)
.data(“item.autocomplete”,item)
.append(“+item.label+”)
.附录(ul);
};
this.button=$(“”)
.attr(“tabIndex”,-1)
.attr(“标题”、“显示所有项目”)
.insertAfter(输入)
.按钮({
图标:{
主要:“ui-icon-triangle-1-s”
},
文本:false
})
.removeClass(“用户界面角全部”)
.addClass(“ui右角ui按钮图标”)
。单击(函数(){
//如果已经可见,请关闭
如果(input.autocomplete(“小部件”)为(“:可见”)){
输入。自动完成(“关闭”);
返回;
}
//解决bug(可能与#5265的原因相同)
$(this.blur();
//将空字符串作为要搜索的值传递,显示所有结果
input.autocomplete(“搜索”和“);
input.focus();
});
},
销毁:函数(){
this.input.remove();
此.button.remove();
this.element.show();
$.Widget.prototype.destroy.call(this);
}
});
})(jQuery);
$(函数(){
//$(“#styleIdPrestazione”).val(“-1”);
$(“#styleIdPrestazione”).combobox();
$(“#切换”)。单击(函数(){
$(“#styleIdPrestazione”).toggle();
});
});
});
在函数中遵循这种方式(我指的是链接而不是隐藏)
将“:selected”设置为(“tabIndex”,-1)
在这个物体的表面上
代码中已经有了这些,只是玩一下而已。
我没有试过,只是快速猜测,希望这是你需要的
select = this.element.hide(),
selected = select.children( ":selected" ),
value = selected.val() ? selected.text() : "";