2011-11-21 94 views
4

我正在使用jQuery autocomplete,它可以与现有元素一起工作,但不能动态添加元素。将jQuery自动完成应用于克隆元素

这里是我的自动完成代码(我已经改变了一下)

$(function() { 

     (function($) { 
     $.widget("ui.combobox", { 
      _create: function() { 
       var self = this, 
        select = this.element.hide(), 
        selected = select.children(":selected"), 
        value = selected.val() ? selected.text() : ""; 
       var input = this.input = $(".editableCombobox") // your input box 
        //.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" 
              ), "$1"), 
             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) { 
            // 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") 
        .live('click',function() { 
         // close if already visible 
         if ($(this).prev().autocomplete("widget").is(":visible")) { 
          $(this).prev().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 
         $(this).prev().autocomplete("search", ""); 
         $(this).prev().focus(); 
        }); 
      }, 

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

     $("#combobox").combobox(); 
     $("#toggle").live('click',function() { 
      $("#combobox").toggle(); 
     }); 

     }); 

这里是我的代码,添加了新的元素

var selectedRow = $('#contactGroup'+rowId); 
    var clonedRow = selectedRow.clone(); 
selectedRow.after(clonedRow); 

阅读我觉得.live可以帮助很多类似的问题,但后不知道在哪里使用它。

编辑:

我试着删除live

为自动完成

$(function() { 

     (function($) { 
     $.widget("ui.combobox", { 
      _create: function() { 
       var self = this, 
        select = this.element.hide(), 
        selected = select.children(":selected"), 
        value = selected.val() ? selected.text() : ""; 
       var input = this.input = $(".editableCombobox") // your input box 
        //.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" 
              ), "$1"), 
             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) { 
            // 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 ($(this).prev().autocomplete("widget").is(":visible")) { 
          $(this).prev().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 
         $(this).prev().autocomplete("search", ""); 
         $(this).prev().focus(); 
        }); 
      }, 

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

     $("#combobox").combobox(); 
     $("#toggle").click(function() { 
      $("#combobox").toggle(); 
     }); 

     }); 

新的代码克隆方法

var selectedRow = $('#contactGroup'+rowId); 
     var clonedRow = selectedRow.clone(); 
    selectedRow.after(clonedRow); 
$(('#contactGroup'+rowId) .editableCombobox).autocomplete("search", ""); 
+0

请告诉我这个问题?你有错误吗?你在哪里启用克隆行的自动完成?' – Jan

+0

@Jan:我试过Thorsten的答案。这些事件不绑定到自动完成。 – xyz

+0

你能举一个完整的例子吗?再说一遍:你如何将自动完成绑定到你的克隆输入? Thorstens jsFiddle适合我! – Jan

回答

8

不能使用“活”上自动完成,据我所知有约束力新添加的元素。

将您的自动填充选项置于一个函数中,该函数需要该字段为参数,您要在其上应用自动填充方法。

function enable_autocomplete(InputField) { 
    $(InputField).autocomplete({ 
     source: availableTags 
    }); 
} 

然后,在克隆字段后,用克隆字段调用该函数。

enable_autocomplete(ClonedField); 

I've written you an easy example, what makes it much easier to understand what I am trying to say ;-)

编辑I've written another example based on the combobox example from jQueryUIs website.

+0

感谢您的建议。我删除了所有'live'方法并在克隆方法$(('#contactGroup'+ rowId).editableCombobox).autocomplete(“search”,“”)中添加了以下代码:'但仍然得到相同的结果。任何其他的事情,我应该尝试? – xyz

+0

请检查我的编辑。 – xyz

+0

我已经添加了另一个小提琴,检查出来。 – Thorsten

1

你可以用 '活',虽然这些日子里,你应该使用 '上' 的方法。

看到这个线程工作示例(包括一个“开”使用,如果你在一些隐藏的答案钻取): Bind jQuery UI autocomplete using .live()

干杯 马特