2011-03-03 47 views
0

我有下面的javascript,它是一個下拉組合框的wannabe實現,它基本上就像一個自動完成,但我添加了一個按鈕,並在點擊事件處理程序中,我有一個困難的時間確切地寫下來。該按鈕的要求是輸出數據庫中所有項目的可滾動列表,如果搜索輸入爲空,否則將搜索結果輸出到相應的搜索項,任何幫助都會執行....非常感謝! :帶有自動完成的不引人注目的jquery

$(document).ready(function(){ 
    $('input[data-ddcombobox]').railsCombobox(); 
}); 

(function(jQuery){ 
    var self = null; 
    $.fn.railsCombobox = function() { 
     return this.live('focus', function(){ 
      if(!this.railsAutoCompleter){ 
       this.railsAutoCompleter = new jQuery.railsCombobox(this); 
      } 
     }); 
    }; 
    jQuery.railsCombobox = function(e){ 
     _e = e; 
     this.init(_e); 
    }; 
    jQuery.railsCombobox.fn = jQuery.railsCombobox.prototype = { 
     railsCombobox: '0.0.1' 
    }; 
    jQuery.railsCombobox.fn.extend = jQuery.railsCombobox.extend = jQuery.extend; 
    jQuery.railsCombobox.fn.extend({ 
     init: function(e){ 
      e.delimiter = $(e).attr('data-delimiter') || null; 
      function split(val){ 
       return val.split(e.delimiter); 
      } 
      function extractLast(term) { 
       return split(term).pop().replace(/^\s+/,""); 
      } 
      $(e).autocomplete({ 
       source: function(request, response){ 
        $.getJSON($(e).attr('data-ddcombobox'), { 
         term: extractLast(request.term) 
         }, response); 
        }, 
       search: function(){ 
        // cusom minLength 
        var term = extractLast(this.value); 
        if(term.length < 2){ 
         return false; 
        } 
       }, 
       focus: function(){ 
        // prevent value inserted on focus 
        return false; 
       }, 
       select: function(event, ui){ 
        var terms = split(this.value); 
        // remove the current input 
        terms.pop(); 
        // add the selected item 
        terms.push(ui.item.value); 
        // add placeholder to get the comma and space at the end 
        if(e.delimiter != null){ 
         term.push(""); 
         this.value = terms.join(e.delimiter); 
        }else{ 
         this.value = terms.join(""); 
         if($(this).attr('id_element')){ 
          $($(this).attr('id_element')).val(ui.item.id); 
         } 
        }; 
        return false; 
       } 
      }); 

      //////////// 

      this.button = $("<button type='button'>&nbsp;</button>") 
       .attr("tabIndex", -1) 
       .attr("title", "Show All Items") 
       .insertAfter(e) 
       .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 
        $(e).autocomplete("search", ""); 
       }); 
      //////////// 
     } 
    }); 
})(jQuery); 

回答

0

$(e).autocomplete(「search」,$(e).val());

是答案。