2012-02-27 81 views
3

我正在使用jQuery Autocomplete但我需要它是可编辑的。我的意思是如果一个值不在列表中,我需要捕获它们输入的值。使用上述链接中的示例,用户可能看不到他们的选择,例如C#,并键入他们的语言。我在默认行为中发现的是,如果在列表中找不到该值,则会清除用户响应。jQuery组合框/自动完成但可编辑

添加以下代码:

options: { 
    allowUserDefined: false 
}, 

var self = this, 
    **options = this.options,** 

if (!valid && !options.allowUserDefined) { 
    // remove invalid value, as it didn't match anything 
    $(this).val(""); 
    select.val(""); 
    input.data("autocomplete").term = ""; 
    return false; 
} 

,以防止被清除的响应。这些改变确实奏效。但是由于在列表中找不到C#,因此没有选定的选项可以返回。

如何让代码返回在输入字段中输入的内容?在调试代码时,特别是在change事件中,input.val()包含输入的文本,这就是我所需要的。我只是无法弄清楚如何获得这个值。

谢谢。

这里是我完整的js文件:

(function ($) { 
    $.widget("ui.combobox", { 
     options: { 
      allowUserDefined: false 
     }, 
     _create: function() { 
      if (this.options.allowUserDefined === null) { 
       this.options.allowUserDefined = false; 
      } 

      var self = this, 
        options = this.options, 
        select = this.element.hide(), 
        selected = select.children(":selected"), 
        value = selected.val() ? selected.text() : ""; 
      var input = this.input = $("<input>") 
        .insertAfter(select) 
        .val(value) 
        .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; 
          self._trigger("selected", event, { 
           item: ui.item.option 
          }); 
         }, 
         change: function (event, ui) { 
          if (!ui.item) { 
           var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i"), 
            valid = false; 
           select.children("option").each(function() { 
            if ($(this).text().match(matcher)) { 
             this.selected = valid = true; 
             return false; 
            } 
           }); 
           if (!valid && !options.allowUserDefined) { 
            // remove invalid value, as it didn't match anything 
            $(this).val(""); 
            select.val(""); 
            input.data("autocomplete").term = ""; 
            return false; 
           } 
          } 
         } 
        }) 
        .addClass("ui-widget ui-widget-content ui-corner-left"); 

      input.data("autocomplete")._renderItem = function (ul, item) { 
       return $("<li></li>") 
         .data("item.autocomplete", item) 
         .append("<a>" + item.label + "</a>") 
         .appendTo(ul); 
      }; 

      this.button = $("<button type='button'>&nbsp;</button>") 
        .attr("tabIndex", -1) 
        .attr("title", "Show All Items") 
        .insertAfter(input) 
        .button({ 
         icons: { 
          primary: "ui-icon-triangle-1-s" 
         }, 
         text: false 
        }) 
        .removeClass("ui-corner-all") 
        .addClass("ui-corner-right ui-button-icon") 
        .click(function() { 
         // close if already visible 
         if (input.autocomplete("widget").is(":visible")) { 
          input.autocomplete("close"); 
          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(); 
        }); 
     }, 

     destroy: function() { 
      this.input.remove(); 
      this.button.remove(); 
      this.element.show(); 
      $.Widget.prototype.destroy.call(this); 
     } 
    }); 
})(jQuery); 

更新

使用Darkajax的代码(我喜欢严格的选项比我想出了更好的),我可以得到的价值输入框这样做:

var valu = $("#combobox").parent().children()[1].value; 
+0

好,我很高兴我能够贡献至少一点! – DarkAjax 2012-02-28 14:12:44

回答

5

我使用jQuery UI自动完成组合框时做我自己是声明组合框wi DGET是这样的:

(function($) { 
    $.widget("ui.combobox", { 
     // default options 
     options: { 
      strict: false 
     }, 
     _create: function() { 
      var self = this, 
       select = this.element.hide(), 
       selected = select.children(":selected"), 
       value = selected.val() ? selected.text() : "", 
       strict = this.options.strict; 

      var input = this.input = $("<input>") 
       .insertAfter(select) 
       .val(value) 
       .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; 
         self._trigger("selected", event, { 
          item: ui.item.option 
         }); 
        }, 
        autocomplete : function(value) { 
         this.element.val(value); 
         this.input.val(value); 
        }, 
        change: function(event, ui) { 
         if (!ui.item) { 
          var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i"), 
           valid = false; 
          select.children("option").each(function() { 
           if (this.value.match(matcher)) { 
            this.selected = valid = true; 
            return false; 
           } 
          }); 
          if (!valid) { 
           // if strict is true, then unmatched values are not allowed 
           if (strict) { 
            // remove invalid value, as it didn't match anything 
            $(this).val(""); 
            select.val(""); 
           } 
           return false; 
          } 
         } 
        } 
       }) 
       .addClass("ui-widget ui-widget-content ui-corner-left"); 

      input.data("autocomplete")._renderItem = function(ul, item) { 
       return $("<li></li>") 
        .data("item.autocomplete", item) 
        .append("<a>" + item.label + "</a>") 
        .appendTo(ul); 
      }; 

      this.button = $("<button type=\"button\" class=combo_button>&nbsp;</button>") 
       .attr("tabIndex", -1) 
       .attr("title", "Show All Items") 
       .insertAfter(input) 
       .button({ 
        icons: { 
         primary: "ui-icon-triangle-1-s" 
        }, 
        text: false 
       }) 
       .removeClass("ui-corner-all") 
       .addClass("ui-corner-right ui-button-icon") 
       .click(function() { 
        // close if already visible 
        if (input.autocomplete("widget").is(":visible")) { 
         input.autocomplete("close"); 
         return; 
        } 

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

这将使接受其他选项的有效值,你就只需要使用:

$("#combobox").combobox({ 
    strict: true 
}); 

为了使其作为默认的工作,以防万一你需要它。

+0

谢谢你的回应darkajax。我仍然无法获取输入的值;它似乎只是返回列表中的第一个项目。我知道必须有一种方式:) – DrZ 2012-02-27 22:41:37

+0

看到我上面的更新。 – DrZ 2012-02-27 23:08:40

0

在你

source: function(request, response) 
{ 

您可以访问request.term =什么类型。

0

使用从darkajax了清晰的解决方案,以及下面的代码行:

var valu = $("#combobox").parent().children()[1].value; 

我能得到的组合框的值。

0

我不知道这是否会帮助任何人,而是通过给输入一个名称,该值被提交....(我删除删除无效条目的代码,当然)

var input = this.input = $("<input name='somehthing'>")