jQuery ui Combobox 扩展

[img]http://dl2.iteye.com/upload/attachment/0099/9490/2cff105c-7e20-3ca0-a9f6-071f4096faac.png[/img]


[b]jQuery ui[/b] 是一个UI的雏形, 一些UI都基于jQuery ui 开发,例如easy ui。

最近项目用jquery ui 做前台,需要用到 combobox组件, 如果用easy ui 需要引入很多其他的js比较麻烦, jquery ui Demo中 autocomplete 里面自带一个Combobox, 但一些功能不是我们想要的。
自己在上面增加了几个方法和事件。 并修改了对IE6支持的CSS 如下:

[b]好处[/b]: 下拉框的样式随jquery ui 的样式改变而改变, 支持自动补全
[b]不足[/b]: 方法需要自己扩展
————————————————————————————————————————
参数:
tip: "提示",
isReadonly: true, //默认下拉框值不可编辑, 如果设置为false, 那么下拉框支持自动补全

增加方法说明:
事件:
1.onChange: 当下拉框修改选中一条时候触发

方法:
主动设置下拉框内容
1.$( "#combobox" ).combobox('setSelect', 'option中value', 'option中text');

————————————————————————————————————————
测试:

Html:
<select id="combobox">
...
</select>

依赖: jquery ui 为1.9.1
需要依赖jquery ui、 jquery
<link rel="stylesheet" type="text/css" href="jquery/jqueryui/css/redmond/jquery-ui.custom.min.css" />
<link rel="stylesheet" type="text/css" href="jquery/jqueryui/combobox/jquery.combobox.css" />

<script src="jquery/jquery.min.js" type="text/javascript"></script>
<script src="jquery/jqueryui/js/jquery-ui.custom.min.js" type="text/javascript"></script>
<script src="jquery/jqueryui/combobox/jquery.combobox.js" type="text/javascript"></script>


$( "#combobox" ).combobox({
tip: "鼠标划入下拉按钮时候会有提示!",
onChange: function(option){
alert($(option).attr('value'));
}
});




(function( $ ) {
$.widget( "ui.combobox", {
version: "1.9.1",
options: {
tip: "",
isReadonly: true,
/**
* This event fire after selected select.
*/
onChange: function(option) {}
},
input: null,
_create: function() {
var 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 + " 没有匹配结果!" )
.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" )
.addClass( "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
});

that.options.onChange(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", this.options.tip )
.tooltip()
.appendTo( wrapper )
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
//移除
// .removeClass( "ui-button" )
.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"
});

if (this.options.isReadonly) {
input.attr('readonly', true);
}
},

destroy: function() {
this.wrapper.remove();
this.element.show();
$.Widget.prototype.destroy.call( this );
},
/**
* Set input value
* @param value option's value
* @param text option's text
*/
setSelect: function(value, text) {
input.val(text);
}
});
})( jQuery );



CSS


.ui-combobox {
position: relative;
display: inline-block;
padding-right: 20px;
}
.ui-combobox-toggle {
position: absolute;
top: 0;
bottom: 0;
margin-left: -1px;
padding: 0 0 0 0;
/* adjust styles for IE 6/7 */
*height: 16px;
*top: 0.1em;
PADDING-RIGHT: 0px;
width: 20px;
}
.ui-combobox-input {
margin: 0;
padding: 0em;
width: 120px;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
jQueryCombobox是一个自定义下拉框(dropdown)插件,它结合了文本输入框和下拉列表的功能。通过使用Combobox插件,你可以创建一个更加灵活和易于使用的下拉框。 在使用Combobox之前,你需要引入jQuery库和Combobox插件。然后,你可以通过以下步骤来创建一个Combobox: 1. 首先,在HTML中创建一个文本输入框和一个隐藏的下拉列表: ```html <input type="text" id="myInput"> <select id="mySelect" style="display: none;"> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select> ``` 2. 接下来,在JavaScript中初始化Combobox插件: ```javascript $(document).ready(function() { $('#myInput').combobox({ select: function(event, ui) { // 当选择了一个项时触发的回调函数 var selectedValue = ui.item.value; // 执行相关操作 } }); }); ``` 3. 最后,你可以自定义Combobox的样式。Combobox默认为文本输入框添加了一个CSS类名为`ui-combobox-input`,你可以使用该类名来自定义样式。 通过上述步骤,你就可以创建一个基本的Combobox。当用户在文本输入框中开始输入时,Combobox会根据输入内容自动过滤下拉列表,并显示匹配的选项。用户可以通过键盘方向键或鼠标来选择一个选项,选择的值会显示在文本输入框中。 希望这个简单的介绍能帮助到你!如有更多问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值