JQuery UI Autocomplete

深藏阁楼爱情的钟 2022-07-12 11:42 316阅读 0赞
  1. <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>jQuery UI Autocomplete - Remote datasource</title> <link rel="stylesheet" href="http://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css"> <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css"> <style> .ui-autocomplete-loading { background: white url("../images/ui-anim_basic_16x16.gif") right center no-repeat; } </style> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script> <script> $(function () { function log(message) { $("<div>").text(message).prependTo("#log"); $("#log").scrollTop(0); } $("#birds").autocomplete({ source: "../handler/CommonHandler.ashx?", minLength: 0, select: function (event, ui) { log(ui.item ? "Selected value = " + ui.item.value + ",id = " + ui.item.id : "Nothing selected, input was " + this.value); event.preventDefault(); $("#birds").val(ui.item.label); }, focus: function (event, ui) { event.preventDefault(); $("#birds").val(ui.item.label); } }); }); </script> </head> <body> <div class="ui-widget"> <label for="birds">Birds: </label> <input id="birds"> </div> <div class="ui-widget" style="margin-top:2em; font-family:Arial"> Result:<div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div> </div> </body> </html>

source:是远程请求地址,请求返回的格式如下:

Center

当你输入关键词时,插件会根据你设置的minLength的限制开始向URL请求,注意URL中会自动添加term参数,如下:

  1. http://localhost:52865/handler/CommonHandler.ashx?&term=k

当你进行上下选择的时候,默认会选择value到input输入框,在select事件中,添加如下代码:

  1. select: function (event, ui) { event.preventDefault(); $("#birds").val(ui.item.label); }, focus: function (event, ui) { event.preventDefault(); $("#birds").val(ui.item.label); }

参考链接:

http://stackoverflow.com/questions/7642855/autocomplete-applying-value-not-label-to-textbox

http://api.jqueryui.com/autocomplete/

发表评论

表情:
评论列表 (有 0 条评论,316人围观)

还没有评论,来说两句吧...

相关阅读