我发现这个“hack”可以将 jTemplates 与 jQuery UI Autocomplete 结合使用:
http://www.shawnmclean.com/blog/2011/02/using-jqueryui-autocomplete-with-jtemplates/ http://www.shawnmclean.com/blog/2011/02/using-jqueryui-autocomplete-with-jtemplates/
但是,有没有办法将官方 jQuery 模板插件与 jQuery UI 自动完成一起使用?我只想使用链接中的演示,但如果可能的话更喜欢更简洁的方法。
(有必要使用模板,因为我在网站的其他地方使用它们,并且希望对自动完成项目使用一致的布局,而不必维护两个版本。)
好的,jQuery UI 让这变得非常简单。从页面上的演示http://jqueryui.com/demos/autocomplete/#custom-data http://jqueryui.com/demos/autocomplete/#custom-data,您只需更改 .data() 调用即可:
//this is the original code in the demo
.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
.appendTo( ul );
};
并将其替换为以下 .data() 调用:
.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "#myTemplate" ).tmpl( item ).appendTo( ul );
};
// template
<script id="myTemplate" type="text/x-jquery-tmpl">
<li><a>${label}<br />${desc}</a></li>
</script>
这是小提琴中的工作代码:http://jsfiddle.net/swatkins/XXeDd/ http://jsfiddle.net/swatkins/XXeDd/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)