jqueryui combobox 问题
jqueryui刚用,不熟,我的目标是一个autocomplete选择一个值后,后面的combobox自动下拉里面有值,现在autocomplete功能已实现,combo...
jqueryui刚用,不熟,我的目标是一个autocomplete选择一个值后,后面的combobox自动下拉里面有值,现在autocomplete功能已实现,combobox死磕没搞出来,请高手给指点,所有的数据均为远程取得,在线等,谢谢,combobox的参考资料好像不太多哦,呵呵,谢谢了,分不多,50分仅表示本人的诚意,对于您提供的帮助,非常感谢!
展开
1个回答
展开全部
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>liuxing test</title>
<style type="text/css">
</style>
<script type="text/javascript"
src="jquery-ui-1.9.1.custom/js/jquery-1.8.2.js"></script>
<script type="text/javascript"
src="jquery-ui-1.9.1.custom/js/jquery-ui-1.9.1.custom.min.js"></script>
<link rel="stylesheet" href="jquery-ui-1.9.1.custom/css/base/jquery.ui.all.css">
<style>
.ui-combobox {
position: relative;
display: inline-block;
}
.ui-combobox-toggle {
position: absolute;
top: 0;
bottom: 0;
margin-left: -1px;
padding: 0;
/* adjust styles for IE 6/7 */
*height: 1.7em;
*top: 0.1em;
}
.ui-combobox-input {
margin: 0;
padding: 0.3em;
}
</style>
<script>
(function( $ ) {
$.widget( "ui.combobox", {
_create: function() {
var input,
that = this,
select = this.element.hide(),
selected = select.children( ":selected" ),
value = selected.val() ? selected.text() : "",
wrapper = this.wrapper = $( "<span>" )
.addClass( "ui-combobox" )
.insertAfter( select );
function removeIfInvalid(element) {
var value = $( element ).val(),
matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( value ) + "$", "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
$( element )
.val( "" )
.attr( "title", value + " didn't match any item" )
.tooltip( "open" );
select.val( "" );
setTimeout(function() {
input.tooltip( "close" ).attr( "title", "" );
}, 2500 );
input.data( "autocomplete" ).term = "";
return false;
}
}
input = $( "<input>" )
.appendTo( wrapper )
.val( value )
.attr( "title", "" )
.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;
that._trigger( "selected", event, {
item: ui.item.option
});
},
change: function( event, ui ) {
if ( !ui.item )
return removeIfInvalid( this );
}
})
.addClass( "ui-widget ui-widget-content ui-corner-left" );
input.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.label + "</a>" )
.appendTo( ul );
};
$( "<a>" )
.attr( "tabIndex", -1 )
.attr( "title", "Show All Items" )
.tooltip()
.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" );
removeIfInvalid( input );
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();
});
input
.tooltip({
position: {
of: this.button
},
tooltipClass: "ui-state-highlight"
});
},
destroy: function() {
this.wrapper.remove();
this.element.show();
$.Widget.prototype.destroy.call( this );
}
});
})( jQuery );
$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$( "#tags" ).autocomplete({
source: availableTags,
//以下实现的是当选择普通autoComplete控件下拉列表元素时
//在ComboBox的选择项中添加这一项
//没有判断ComboBox中是否已经存在,所以会出现重复 etc.
select: function( event, ui ) {
var label=ui.item.label,value=ui.item.value;
$( "#combobox" ).append($('<option>').attr('value',value).html(label));
}
});
$( "#combobox" ).combobox();
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
<div class="ui-widget">
<label>ComboBox: </label>
<select id="combobox">
</select>
</div>
</body>
</html>
我猜你想要实现的是选择普通AutoComplete控件下拉列表的选项时 将这一项添加到后边的ComboBox下拉菜单中去.
如果你不是要实现这种效果,你得详细解释下"后面的combobox自动下拉里面有值"这句话.
你需要的代码在191行中文注释下方,有问题请追问.
更多追问追答
追问
我的意思是combobox里的数据是通过前面那个autocomplete的结果,然后从数据库中查出来的
主要combobox那段,给段远程取得数据的代码参考一下,谢谢!
追答
假设你使用java-servlet实现的后台查询,URL是"yourUrl.action",接受一个叫label的参数,查询到数据后给页面返回一个JSON字符串,格式为[{'label':xxx,'value':xxx},.....]这样子的话,那你在AutoComplete的select事件中这样写
select: function( event, ui ) {
var label=ui.item.label,value=ui.item.value;
//清空Combobox
$( "#combobox" ).empty();
/**此处传递上面的参数进行查询,获得远程数据*/
//假设你用java-servlet实现的后台功能,
$.ajax({
url:"yourUrl.action",
data:{'label':label},
type:'POST',
dataType:'json',
success:function(data){
if(data && data.length>0){
for(var index=0;index<data.length;index++){
//将选项添加到comboBox中
$( "#combobox" ).append($('<option>').attr('value',data[i].value).html(data[i].label));
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询