Reputation: 966
i use jquery, autocomplete and dinamicly created input nodes.
my first input have connection:
var autocomp_art_opt = {
source: "ajax_find_art.php",
minLength: 2,
select: function(event, ui) {
$.ajax({
type: "GET",
url: "ajax_find_price.php",
data: "art="+ui.item.value,
cache: false,
success: function(data){
$("#price1").val(data);
}
});
}
};
$("#art1").autocomplete(autocomp_art_opt);
i have a script for add two input fields (#art{number} and #price{number}) and connect new autocomplete:
$("#art2").autocomplete(autocomp_art_opt);
$("#art3").autocomplete(autocomp_art_opt);
... etc
but i cant chenge #price1 in autocomp_art_opt whithout create new variable autocomp_art_opt{number}...
how to know object id (#art{number}) in function:
success: function(data){
$("#price1").val(data);
}
how to change #price1 to #price5 when function called for #art5 and etc...
Upvotes: 0
Views: 52
Reputation: 27247
The select
callback has references to event
and ui
. The event
variable should have contain event.target
, which should be a reference to the box which was clicked, e.g. $("#art2").autocomplete(autocomp_art_opt);
would refer to $("#art2")
.
Upvotes: 1
Reputation: 82277
Create some functions to wrap your object and calls in, and then you can simply call the initializer with the number of art/price.
call
initializeAutocomplete(1);//$("#art1") and $("#price1")
initializeAutocomplete(5);//$("#art5") and $("#price5")
setup
function autocomp_art_opt(number){
return {
source: "ajax_find_art.php",
minLength: 2,
select: function(event, ui) {
$.ajax({
type: "GET",
url: "ajax_find_price.php",
data: "art="+ui.item.value,
cache: false,
success: function(data){
$("#price"+number).val(data);
}
});
}
};
}
function initializeAutocomplete(num){
$("#art"+num).autocomplete(autocomp_art_opt(num));
}
Upvotes: 1