1

我正在尝试在自动完成列表的末尾添加一个附加标签。

$('#address ul.ui-autocomplete').append("<li>Add Venue</li>");

我试图弄清楚我可以在哪里放置上面的代码以将额外的 li 添加到自动完成列表中。

任何帮助将不胜感激。

这是 rails3-jquery-autocomplete 文件。

  source: function( request, response ) {
  $.getJSON( $(e).attr('data-autocomplete'), {
    term: extractLast( request.term )
  }, function() {
    $(arguments[0]).each(function(i, el) {
      var obj = {};
      obj[el.id] = el;
      $(e).data(obj);
    });
    response.apply(null, arguments);
  });
},

open: function() {
  // when appending the result list to another element, we need to cancel the "position: relative;" css.
 if (append_to){
   $(append_to + ' ul.ui-autocomplete').css('position', 'static');

  } 
},
search: function() {
  // custom minLength

  var minLength = $(e).attr('min_length') || 2;
  var term = extractLast( this.value );
  if ( term.length < minLength ) {
    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) {
    terms.push( "" );
    this.value = terms.join( e.delimiter );
  } else {
    this.value = terms.join("");
    if ($(this).attr('data-id-element')) {
      $($(this).attr('data-id-element')).val(ui.item.id);

    }
    if ($(this).attr('data-update-elements')) {
      var data = $(this).data(ui.item.id.toString());
      var update_elements = $.parseJSON($(this).attr("data-update-elements"));
      for (var key in update_elements) {
        $(update_elements[key]).val(data[key]);
      }
    }
  }
  var remember_string = this.value;
  $(this).bind('keyup.clearId', function(){
    if($(this).val().trim() != remember_string.trim()){
      $($(this).attr('data-id-element')).val("");
      $(this).unbind('keyup.clearId');
    }
  });
  $(this).trigger('railsAutocomplete.select');
  return false;


}

}); }

4

1 回答 1

2

用这个解决了。

$('#address').bind('autocompleteopen', function(event,data){

$('<li id="ac-add-venue"><a href="....">Add venue</a></li>').appendTo('ul.ui-autocomplete');

});

于 2012-04-13T22:15:58.833 回答