取自jQuery autocomplete中的jQuery自动填充文档:
instance()
Retrieves the autocomplete's instance object. If the element does not have an associated instance, undefined is returned.
Unlike other widget methods, instance() is safe to call on any element after the autocomplete plugin has loaded.
This method does not accept any arguments.
Code examples:
Invoke the instance method:
$( ".selector" ).autocomplete( "instance" );
好的,那是什么意思?我会用它做什么用的?我正在重构别人的javascript代码,这里有一个使用它的例子:
ProjectForm = {
init: function() {
// This code removes a selected license
$(document).delegate( 'a.remove_license', 'click', function() {
$(this).closest('.license').remove();
if( $('.chosen_licenses').html() === '' ) {
$('.chosen_licenses').html('<div class="license inset">[None]</div>');
}
return false;
});
this.license_autocomplete();
},
license_autocomplete: function() {
// This code performs the autocomplete and adds html element
$('#add_license').autocomplete({
source: '/autocompletes/licenses',
select: function(event, ui) {
if( $.trim($('.chosen_licenses div:first').html()) === '[None]' ) {
$('.chosen_licenses').html('');
}
var html = (''+
'<div class="license col-md-5 no_margin_left">'+
' <div class="col-md-6">#{name}</div>'+
' <div class="col-md-5 pull-right" style="margin: 0 20px 20px 0">'+
' <a href="#" class="btn btn-danger btn-mini remove_license col" data_id="#{id}">'+
' <i class="icon-trash"></i> Remove</a>'+
' </div>'+
'</div>')._f(ui.item);
$('.chosen_licenses').append(html);
}
})
.autocomplete( "instance" )
._renderItem = function(ul, item) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<p>" + item.name + "</p>" )
.appendTo( ul );
};
}
}
如果删除该行,则source
的结果不会显示。有人可以告诉我它的作用吗?