C# jQuery UI自动完成组合框远程数据源
我试图实现jQueryUIAutoComplete组合框,以便它在ASP.NET/C中可重用 我在使用基本的自动完成功能时没有遇到任何问题,但是当我尝试使用组合框时,我感到困惑 当返回JSON中的键/值对时,我有一个处理程序 然后,我需要多个组合框来检索不同的数据。我只需要一个处理程序,所以我想通过source方法在正在使用的控件的querystring中传递一个引用 我真正找到的唯一一个例子是我想要的,但它对我来说并不正确,而且它不可重用,而且似乎有一些重复的代码 jQuery站点上的示例如下,但是当将源更改为使用远程源时,组合框永远不会填充C# jQuery UI自动完成组合框远程数据源,c#,jquery,asp.net,jquery-ui,jquery-ui-autocomplete,C#,Jquery,Asp.net,Jquery Ui,Jquery Ui Autocomplete,我试图实现jQueryUIAutoComplete组合框,以便它在ASP.NET/C中可重用 我在使用基本的自动完成功能时没有遇到任何问题,但是当我尝试使用组合框时,我感到困惑 当返回JSON中的键/值对时,我有一个处理程序 然后,我需要多个组合框来检索不同的数据。我只需要一个处理程序,所以我想通过source方法在正在使用的控件的querystring中传递一个引用 我真正找到的唯一一个例子是我想要的,但它对我来说并不正确,而且它不可重用,而且似乎有一些重复的代码 jQuery站点上的示例如下
(function( $ ) {
$.widget( "ui.combobox", {
_create: function() {
var input,
self = this,
select = this.element.hide(),
selected = select.children( ":selected" ),
value = selected.val() ? selected.text() : "",
wrapper = this.wrapper = $( "<span>" )
.addClass( "ui-combobox" )
.insertAfter( select );
input = $( "<input>" )
.appendTo( wrapper )
.val( value )
.addClass( "ui-state-default ui-combobox-input" )
.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 ) {
// 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 );
};
$( "<a>" )
.attr( "tabIndex", -1 )
.attr( "title", "Show All Items" )
.appendTo( wrapper )
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass( "ui-corner-all" )
.addClass( "ui-corner-right ui-combobox-toggle" )
.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.wrapper.remove();
this.element.show();
$.Widget.prototype.destroy.call( this );
}
});
})( jQuery );
$(function() {
$( "#combobox" ).combobox();
$( "#toggle" ).click(function() {
$( "#combobox" ).toggle();
});
});
(函数($){
$.widget(“ui.combobox”{
_创建:函数(){
var输入,
self=这个,
select=this.element.hide(),
selected=select.children(“:selected”),
value=selected.val()?selected.text():“”,
包装器=此。包装器=$(“”)
.addClass(“ui组合框”)
.insertAfter(选择);
输入=$(“”)
.appendTo(包装器)
.val(值)
.addClass(“ui状态默认ui组合框输入”)
.自动完成({
延迟: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;
自触发(“选定”,事件{
项目:ui.item.option
});
},
更改:功能(事件、用户界面){
如果(!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);
};
$( "" )
.attr(“tabIndex”,-1)
.attr(“标题”、“显示所有项目”)
.appendTo(包装器)
.按钮({
图标:{
主要:“ui-icon-triangle-1-s”
},
文本:false
})
.removeClass(“用户界面角全部”)
.addClass(“ui右角ui组合框切换”)
。单击(函数(){
//如果已经可见,请关闭
如果(input.autocomplete(“小部件”)为(“:可见”)){
输入。自动完成(“关闭”);
返回;
}
//解决bug(可能与#5265的原因相同)
$(this.blur();
//将空字符串作为要搜索的值传递,显示所有结果
input.autocomplete(“搜索”和“);
input.focus();
});
},
销毁:函数(){
this.wrapper.remove();
this.element.show();
$.Widget.prototype.destroy.call(this);
}
});
})(jQuery);
$(函数(){
$(“#组合框”).combobox();
$(“#切换”)。单击(函数(){
$(“#组合框”).toggle();
});
});
理想情况下,在这个阶段:$(“#combobox”).combobox()
我想指定我的远程数据源,但在尝试时,即$(“#combobox”).combobox({source:“handler.ashx”})代码>组合框从不填充。只有编辑上面的autocomplete
部分并删除所有regex
代码,我才能让它工作,但是如何引用当前的组合框来获取它的值呢?我真的很困惑,这一定很简单,我把事情搞得太复杂了。。。需要帮忙吗
(function ($) {
$.widget("ui.combobox", {
_create: function() {
var wrapper = this.wrapper = $("<span />").addClass("ui-combobox")
, self = this;
this.element.wrap(wrapper);
this.element
.addClass("ui-state-default ui-combobox-input ui-widget ui-widget-content ui-corner-left")
.autocomplete($.extend({
minLength: 0
}, this.options));
$("<a />")
.insertAfter(this.element)
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass("ui-corner-all")
.addClass("ui-corner-right ui-combobox-toggle")
.click(function () {
if (self.element.autocomplete("widget" ).is(":visible")) {
self.element.autocomplete("close");
return;
}
$(this).blur();
self.element.autocomplete("search", "");
self.element.focus();
});
},
destroy: function() {
this.wrapper.remove();
$.Widget.prototype.destroy.call(this);
}
});
})(jQuery);
$(function() {
$("#combo").combobox({
source: 'my_source'
});
});