I'm have this code below. As you can see I'm trying to use the variable clients in alert function of jqueryautocomplete.
The problem: when i select an item from the autocomplete drop-down menu it prints "undefined" instead of the value of the the var. Why?
jQuery(document).ready(function() {
var clients = 0;
alert(clients); //this prints 0.
jQuery("#user_autocomplete")
.autocomplete('autocompleteUser', jQuery.extend({}, {
dataType: 'json',
parse: function(data) {
var parsed = [];
for (key in data) {
parsed[parsed.length] = { data: [ data[key], key ], value: data[key], result: data[key] };
}
return parsed;
}
} ))
.result(function(event, data) {
$('#field_开发者_JAVA技巧users').append('<div class="user_choosen" id=' + data[1] + '>' + data[0] +'<a class="link_delete_user" href="#" onclick="javascript:deleteUser(' + data[1] +')">Delete</a></div>');
$('#user_autocomplete').val('');
alert(clients); //this prints undefined
});
});
Regards
Javi
That's not a global variable, that's a variable defined in the scope of the anonymous function run when the document is ready.
I believe that scope may lost when running the anonymous function passed into results method of the auto complete.
精彩评论