[JavaScript] [jQuery]一个更好的 jQuery UI 自动完成的 Combo Box →→→→→进入此内容的聊天室

来自 , 2021-03-12, 写在 JavaScript, 查看 109 次.
URL http://www.code666.cn/view/fd9042c9
  1. (function ($) {
  2.     $.widget("ui.combobox", {
  3.         _create: function () {
  4.             var self = this,
  5.                 select = this.element.hide(),
  6.                 selected = select.children(":selected"),
  7.                 value = selected.val() ? selected.text() : "",
  8.                 regSearch = /^[^a-zA-Z0-9]*([a-zA-Z0-9])/i,
  9.                 comboData = select.children("option").map(function () {
  10.                         if (this.value ) {
  11.                                 var text = $(this).text(),
  12.                                         labelHtml = self.options.label ? self.options.label(this) : text; //allows list customization
  13.  
  14.                                 return {
  15.                                         label: labelHtml,
  16.                                         value: text,
  17.                                         option: this
  18.                                 };
  19.                         }
  20.                 });
  21.  
  22.             var input = this.input = $("<input type='text' />")
  23.                                         .insertAfter(select)
  24.                                         .val(value)
  25.                                         .keydown( function( event ) {
  26.                                         var keyCode = $.ui.keyCode;
  27.                                         switch( event.keyCode ) {
  28.                                                 case keyCode.PAGE_UP:
  29.                                                 case keyCode.PAGE_DOWN:
  30.                                                 case keyCode.UP:
  31.                                                 case keyCode.DOWN:
  32.                                                 case keyCode.ENTER:
  33.                                                 case keyCode.NUMPAD_ENTER:
  34.                                                 case keyCode.TAB:
  35.                                                 case keyCode.ESCAPE:
  36.                                                         //let autocomplete handle these
  37.                                                         break;
  38.                                                 default:
  39.                                                         //prevent autocomplete doing anything
  40.                                                         event.stopImmediatePropagation();
  41.                                                         //only react to [a-zA-Z0-9]
  42.                                                         if ((event.keyCode < 91 && event.keyCode > 59)
  43.                                                                 || (event.keyCode < 58 && event.keyCode > 47)) {
  44.  
  45.                                                                 var str = String.fromCharCode(event.keyCode).toLowerCase(), currVal = input.val(), opt;
  46.  
  47.                                                                 //find all options whose first alpha character matches that pressed
  48.                                                                 var matchOpt = select.children().filter(function() {
  49.                                                                         var test = regSearch.exec(this.text);
  50.                                                                         return (test && test.length == 2 && test[1].toLowerCase() == str);
  51.                                                                 });
  52.  
  53.                                                                 if (!matchOpt.length ) return false;
  54.  
  55.                                                                 //if there is something selected we need to find the next in the list
  56.                                                                 if (currVal.length) {
  57.                                                                         var test = regSearch.exec(currVal);
  58.                                                                         if (test && test.length == 2 && test[1].toLowerCase() == str) {
  59.                                                                                 //the next one that begins with that letter
  60.                                                                                 matchOpt.each(function(ix, el) {
  61.                                                                                         if (el.selected) {
  62.                                                                                                 if ((ix + 1) <= matchOpt.length-1) {
  63.                                                                                                         opt = matchOpt[ix + 1];
  64.                                                                                                 }
  65.                                                                                                 return false;
  66.                                                                                         }
  67.                                                                                 });
  68.                                                                         }
  69.                                                                 }
  70.  
  71.                                                                 //fallback to the first one that begins with that character
  72.                                                                 if (!opt)
  73.                                                                         opt = matchOpt[0];
  74.  
  75.                                                                 //select that item
  76.                                                                 opt.selected = true;
  77.                                                                 input.val(opt.text);
  78.  
  79.                                                                 //if the dropdown is open, find it in the list
  80.                                                                 if (input.autocomplete("widget").is(":visible")) {
  81.                                                                         input.data("autocomplete").widget().children('li').each(function() {           
  82.                                                                                 var $li = $(this);
  83.                                                                                 if ($li.data("item.autocomplete").option == opt) {
  84.                                                                                         input.data("autocomplete").menu.activate(event,$li);
  85.                                                                                         return false;
  86.                                                                                 }
  87.                                                                         });
  88.                                                                 }
  89.                                                         }
  90.                                                         //ignore all other keystrokes
  91.                                                         return false;
  92.                                                         break;
  93.                                                 }
  94.                                           })
  95.                                         .autocomplete({
  96.                                             delay: 0,
  97.                                             minLength: 0,
  98.                                             source: function (request, response) { response(comboData); },
  99.                                             select: function (event, ui) {
  100.                                                 ui.item.option.selected = true;
  101.                                                 self._trigger("selected", event, {
  102.                                                     item: ui.item.option
  103.                                                 });
  104.                                             },
  105.                                             change: function (event, ui) {
  106.                                                         if (!ui.item) {                                
  107.                                                                 var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i"),
  108.                                                                         valid = false;
  109.                                                                 select.children("option").each(function () {
  110.                                                                         if ($(this).text().match(matcher)) {
  111.                                                                                 this.selected = valid = true;
  112.                                                                                 return false;
  113.                                                                         }
  114.                                                                 });
  115.                                                                 if (!valid) {
  116.                                                                         // remove invalid value, as it didn't match anything
  117.                                                                         $(this).val("");
  118.                                                                         select.val("");
  119.                                                                         input.data("autocomplete").term = "";
  120.                                                                         return false;
  121.                                                                 }
  122.                                                         }
  123.                                             }
  124.                                         })
  125.                                         .addClass("ui-widget ui-widget-content ui-corner-left")
  126.                                         .click(function() { self.button.click(); })
  127.                                         .bind("autocompleteopen", function(event, ui){
  128.                                                 //find the currently selected item and highlight it in the list
  129.                                                 var opt = select.children(":selected")[0];
  130.                                                 input.data("autocomplete").widget().children('li').each(function() {           
  131.                                                         var $li = $(this);
  132.                                                         if ($li.data("item.autocomplete").option == opt) {
  133.                                                                 input.data("autocomplete").menu.activate(event,$li);
  134.                                                                 return false;
  135.                                                         }
  136.                                                 });
  137.                                         });
  138.  
  139.             input.data("autocomplete")._renderItem = function (ul, item) {
  140.                 return $("<li></li>")
  141.                                         .data("item.autocomplete", item)
  142.                                         .append("<a href='#'>" + item.label + "</a>")
  143.                                         .appendTo(ul);
  144.             };
  145.  
  146.             this.button = $("<button type='button'>&nbsp;</button>")
  147.                                         .attr("tabIndex", -1)
  148.                                         .attr("title", "Show All Items")
  149.                                         .insertAfter(input)
  150.                                         .button({
  151.                                             icons: {
  152.                                                 primary: "ui-icon-triangle-1-s"
  153.                                             },
  154.                                             text: false
  155.                                         })
  156.                                         .removeClass("ui-corner-all")
  157.                                         .addClass("ui-corner-right ui-button-icon")
  158.                                         .click(function () {
  159.                                             // close if already visible
  160.                                             if (input.autocomplete("widget").is(":visible")) {
  161.                                                 input.autocomplete("close");
  162.                                                 return;
  163.                                             }
  164.  
  165.                                             // pass empty string as value to search for, displaying all results
  166.                                             input.autocomplete("search", "");
  167.                                             input.focus();
  168.                                         });
  169.         },
  170.  
  171.                 //allows programmatic selection of combo using the option value
  172.         setValue: function (value) {
  173.             var $input = this.input;
  174.             $("option", this.element).each(function () {
  175.                 if ($(this).val() == value) {
  176.                     this.selected = true;
  177.                     $input.val(this.text);
  178.                                         return false;
  179.                 }
  180.             });
  181.         },
  182.  
  183.         destroy: function () {
  184.             this.input.remove();
  185.             this.button.remove();
  186.             this.element.show();
  187.             $.Widget.prototype.destroy.call(this);
  188.         }
  189.     });
  190. })(jQuery);
  191. //javascript/1081

回复 "[jQuery]一个更好的 jQuery UI 自动完成的 Combo Box"

这儿你可以回复上面这条便签

captcha