2014-01-28 89 views
3

我正在使用JQuery UI Autocomplete combobox如何动态填充JQuery自动完成组合框?

我有一个简单的选择是这样的:

<select id="myselect" class="combobox">  
<option value="" ></option> 
</select>" 

页面加载时我称之为:

$('.combobox').combobox(); 

我需要做的就是填充组合框的页面加载后。这听起来像是一件微不足道的任务,但是我在最后一天试图让自己的头靠在墙上。

即使当我尝试使用简单的追加像这样:

$("#myselect").append('<option value="value1">text 1</option>') 

值没有出现在组合框中。上面的调用必须是错误的,但我不知道如何将值附加到组合框。

请给我一个方法来做到这一点。

你的帮助是非常赞赏

这是我combobox.js文件的源代码:

(function ($) { 
$.widget("ui.combobox", { 

    _create: function() { 
     var select = this.element; 
     select = this.element; 
     var watermark = "Please select a restaurant..."; 
     select.hide(); 

     // process select options into an array 
     var opts = new Array(); 

     $('option', select).each(function (index) { 
      var opt = new Object(); 
      opt.value = $(this).val(); 
      if ($(this).text() != "") { 
       opt.label = $(this).text(); 
       opts[opts.length] = opt; 
      } 
     }); 

     // set up input text element 
     var input = $("<input class='combo_input' type='text'>"); 

     input.insertAfter(select); 
     input.autocomplete({ 
      source: opts, 
      delay: 0, 
      change: function (event, ui) { 
       if (!ui.item) { 
        // user didn't select an option, but what they typed may still match 
        var enteredString = $(this).val(); 
        var stringMatch = false; 
        for (var i = 0; i < opts.length; i++) { 
         if (opts[i].label.toLowerCase() == enteredString.toLowerCase()) { 
          select.val(opts[i].value);// update (hidden) select 
          //$(this).val(opts[i].label);// corrects any incorrect case 
          //opts[i].css("border", "1px solid red"); 
          stringMatch = true; 

          // Trigger the custom changed event 
          self._trigger("changed", event, { 
           value: select.val() 
          }); 
          break; 
         } 
        } 
        if (!stringMatch) { 
         // remove invalid value, as it didn't match anything 
         $(':selected', select).text(""); 
         $(this).val($(':selected', select).text()).addClass('watermark'); 

        } 
        return false; 
       } 
      }, 
      select: function (event, ui) { 
       select.val(ui.item.value);// update (hidden) select 
       $(this).val(ui.item.label); 
       if ($(this).val() == watermark) { 
        input.addClass('watermark'); 
        imgbt.addClass("invisibility"); 
       } 
       else { 
        input.removeClass('watermark'); 
        imgbt.removeClass("invisibility"); 
       } 
       // Trigger the selected event 
       ui.item.selected = true; 
       self._trigger("selected", event, { 
        value: ui.item.value 
       }); 
       return false; 
      }, 
      focus: function (event, ui) { 
       if (event.which === 38 || event.which === 40) { 
        $(this).val(ui.item.label); 
        return false; 
       } 

      }, 
      // stop parent form from being while menu is open 
      open: function (event, ui) { 
       input.attr("menustatus", "open"); 

      }, 
      close: function (event, ui) { 
       input.attr("menustatus", "closed"); 
      }, 
      autoFocus: true, 
      minLength: 0 
     }); 

     input.addClass("ui-widget"); 

     // initialise text with what's currently selected 

     if ($(':selected', select).val() == "") { 
      input.val(watermark).addClass('watermark') 
     } else { 
      input.val($(':selected', select).text()).removeClass('watermark'); 
     } 
     input.attr('title', input.val()); 


     // lost focus status    

     input.blur(function (e) { 
      if (input.val() == "") { 
       input.val(watermark).addClass('watermark'); 
       imgbt.addClass("invisibility"); 
      } 
      else { 
       input.removeClass('watermark') 
       imgbt.removeClass("invisibility"); 
      } 
     }); 

     //clear text when user clicks in text input    
     input.click(function() { 
      if ($(this).val() == watermark) { 
       $(this).removeClass('watermark').val(""); 
       imgbt.addClass("invisibility"); 
      } else { 
       imgbt.removeClass("invisibility"); 
      } 

     }); 


     // over-ride form submit, so it cant submit if the menu is open 
     input.attr("menustatus", "closed"); 
     var form = $(input).parents('form:first'); 
     $(form).submit(function (e) { 
      return (input.attr('menustatus') == 'closed'); 
     }); 

     var imgbt = $("<button type=\"button\">&nbsp;</button>"); 
     imgbt.addClass('clear_bt').addClass('btn'); 
     if (input.val() == watermark) { imgbt.addClass('invisibility') } 
     else { imgbt.removeClass('invisibility') } 
     imgbt.insertAfter(input); 

     // Clear all the keywords 
     imgbt.click(function (e) { 
      input.val(''); 
      $("body").focus(); 
      $(this).addClass('invisibility'); 
      $("#rcbRestaurant").val(""); 
     }); 
     imgbt.blur(function (e) { 
      input.blur(); 
     }); 

     // set up button for fake 'select' 

     var btn = $("<button>&nbsp;</button>"); 
     btn.attr("tabIndex", -1); 
     btn.attr("title", "Select your option"); 
     btn.addClass("combobox_bt"); 
     btn.insertAfter(imgbt); 
     if (select.hasClass('disabled')) { 
      input.addClass('disabled').attr('disabled', 'disabled'); 
      btn.addClass('disabled').attr('disabled', 'disabled'); 
     } 
     if (select.hasClass('error')) { 
      input.addClass('error').focus(); 
      btn.addClass('error').focus(); 
     } else { input.removeClass('error'); btn.removeClass('error'); } 

     if (select.attr('autofocus') == 'autofocus') { 
      input.focus(); 
      btn.focus(); 
     } 

     btn.button({ 
      icons: { 
       primary: "ui-icon-triangle-1-s " 
      }, 
      text: false 
     }); 
     btn.removeClass("ui-corner-all"); 
     btn.addClass("ui-corner-right ui-button-icon"); 
     btn.click(function() { 
      btn.focus(); 
      input.click(); 
      //event.preventDefault(); 
      // close if already visible 
      if (input.autocomplete("widget").is(":visible")) { 
       input.autocomplete("close"); 
       return false; // return false, so form isn't automatically submitted 
      } 
      // pass empty string as value to search for, displaying all results 
      input.autocomplete("search", ""); 
      input.focus(); 
      return false; // return false, so form isn't automatically submitted 
     }); 

     // add some styles 
     btn.css("z-index", "1"); 
     btn.css("display", "inline"); 
     btn.css("padding", 0); 
     $('span.ui-button-text', btn).css("padding", 0); 

     // for testing 
     /* 
     autocomplete: function(value) { 
      this.element.val(value); 
      this.input.val(value); 
     } 
     */ 
    } 
}); 
})(jQuery); 

回答

0

使用jQuery UI的自动完成功能,它更容易

http://jqueryui.com/autocomplete/

+0

感谢您的快速反应。这就是我正在使用的。我正在使用自动完成组合框(作为我的问题中的链接)。 – seemorgh

+0

您不应该调用组合框来创建窗口小部件,而是使用.autocomplete,页面上有演示,尝试过它们吗? – vasu

+0

根据演示中的源代码(在页面底部查看源代码链接 - 请参阅我的问题中的链接),附加组合框的调用是这样的:$(“#combobox”).combobox(); – seemorgh

1

我过去曾使用Chosen来完成此操作。可能为你工作?

+0

嗨Wefx。谢谢你快速的回复。我可以试试,但JQuery UI组合框是我的客户使用的,他们宁愿保持一致性。有什么方法可以轻松实现我所要求的吗?它不应该那么难! – seemorgh

2

jsFiddle Demo

你链接的演示,以及combobox你打电话时,显示的“自动完成什么可以”自定义实现。为了使用这个功能,你必须编写你自己的组合框。这是该演示的重点。所以对于你来说,你需要必须实现你自己的combobx部件才能使用它的功能。没有“combobox”标准的jQuery UI,只有自动完成。请注意,您将收到错误消息:

Uncaught TypeError: Object [object Object] has no method 'combobox'

不使用自定义实现。基本上,你应该复制粘贴小部件,然后使用它或修改它为自己的用途。

(function($) { 
$.widget("custom.combobox", { 
    _create: function() { 
    this.wrapper = $("<span>") 
     .addClass("custom-combobox") 
     .insertAfter(this.element); 

    this.element.hide(); 
    this._createAutocomplete(); 
    this._createShowAllButton(); 
    }, 

    _createAutocomplete: function() { 
    var selected = this.element.children(":selected"), 
     value = selected.val() ? selected.text() : ""; 

    this.input = $("<input>") 
     .appendTo(this.wrapper) 
     .val(value) 
     .attr("title", "") 
     .addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left") 
     .autocomplete({ 
     delay: 0, 
     minLength: 0, 
     source: $.proxy(this, "_source") 
     }) 
     .tooltip({ 
     tooltipClass: "ui-state-highlight" 
     }); 

    this._on(this.input, { 
     autocompleteselect: function(event, ui) { 
     ui.item.option.selected = true; 
     this._trigger("select", event, { 
      item: ui.item.option 
     }); 
     }, 

     autocompletechange: "_removeIfInvalid" 
    }); 
    }, 

    _createShowAllButton: function() { 
    var input = this.input, 
     wasOpen = false; 

    $("<a>") 
     .attr("tabIndex", -1) 
     .attr("title", "Show All Items") 
     .tooltip() 
     .appendTo(this.wrapper) 
     .button({ 
     icons: { 
      primary: "ui-icon-triangle-1-s" 
     }, 
     text: false 
     }) 
     .removeClass("ui-corner-all") 
     .addClass("custom-combobox-toggle ui-corner-right") 
     .mousedown(function() { 
     wasOpen = input.autocomplete("widget").is(":visible"); 
     }) 
     .click(function() { 
     input.focus(); 

     // Close if already visible 
     if (wasOpen) { 
      return; 
     } 

     // Pass empty string as value to search for, displaying all results 
     input.autocomplete("search", ""); 
     }); 
    }, 

    _source: function(request, response) { 
    var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); 
    response(this.element.children("option").map(function() { 
     var text = $(this).text(); 
     if (this.value && (!request.term || matcher.test(text))) 
     return { 
      label: text, 
      value: text, 
      option: this 
     }; 
    })); 
    }, 

    _removeIfInvalid: function(event, ui) { 

    // Selected an item, nothing to do 
    if (ui.item) { 
     return; 
    } 

    // Search for a match (case-insensitive) 
    var value = this.input.val(), 
     valueLowerCase = value.toLowerCase(), 
     valid = false; 
    this.element.children("option").each(function() { 
     if ($(this).text().toLowerCase() === valueLowerCase) { 
     this.selected = valid = true; 
     return false; 
     } 
    }); 

    // Found a match, nothing to do 
    if (valid) { 
     return; 
    } 

    // Remove invalid value 
    this.input 
     .val("") 
     .attr("title", value + " didn't match any item") 
     .tooltip("open"); 
    this.element.val(""); 
    this._delay(function() { 
     this.input.tooltip("close").attr("title", ""); 
    }, 2500); 
    this.input.data("ui-autocomplete").term = ""; 
    }, 

    _destroy: function() { 
    this.wrapper.remove(); 
    this.element.show(); 
    } 
}); 
})(jQuery); 

与造型

.custom-combobox { 
position: relative; 
display: inline-block; 
} 
.custom-combobox-toggle { 
position: absolute; 
top: 0; 
bottom: 0; 
margin-left: -1px; 
padding: 0; 
/* support: IE7 */ 
height: 1.7em; 
top: 0.1em; 
} 
.custom-combobox-input { 
margin: 0; 
padding: 0.3em; 
} 

做这一切都将让你简单地使用

$("#myselect").append('<option value="value1">text 1</option>'); 
$('#myselect').combobox(); 
$("#myselect").append('<option value="value2">text 2</option>'); 
+0

感谢Travis J.我不确定你的意思。我已经下载并包含在我的项目中,否则我该如何使用它?我也对它做了一些修改,但不能做你在最后几行提到的内容。但是,我的版本(除了我的更改外)与您在此发布的版本之间存在一些差异。我会尝试你的版本,如果它的工作,可能需要添加我的更改。但我只是好奇为什么我的不工作。 – seemorgh

+0

@seemorgh - 当你第一次发布你的问题时,很难得出确实的情况,因为它没有你的源代码。我看到你已经在你的源代码中进行了编辑,并将发布一个编辑到我的答案。 –