2011-11-21 95 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()

乾杯 馬特