Подтвердить что ты не робот

JQuery: Возможно динамическое изменение источника виджета Autocomplete?

Привет,

Я использую официальный виджет автозаполнения jquery, и у меня возникают проблемы с динамическим изменением переменной (selectType), которую я передаю через строку запроса. Переменная будет меняться в зависимости от того, какая опция выбрана с помощью окна выбора.

$(function() {
var selectType = $('#selectType option:selected').attr("value");    


$("#selectType").change(function(){
    selectType = $('#selectType option:selected').attr("value");
    alert (selectType);  // alerts the right value for debugging
});

$("#address").autocomplete({
    source: "ajaxSearchForClientAddress.php?selectType="+selectType,
    minLength: 3
}); 
});
4b9b3361

Ответ 1

Попробуйте изменить параметр source автозаполнения в событии изменения.

$(function () {
    var select = $( "#selectType" ),
        options = select.find( "option" ),
        address = $( "#address" );

    var selectType = options.filter( ":selected" ).attr( "value" );
    address.autocomplete({
        source: "ajaxSearchForClientAddress.php?selectType=" + selectType,
        minLength: 3
    });

    select.change(function () {
        selectType = options.filter( ":selected" ).attr( "value" );
        address.autocomplete( "option", "source", "ajaxSearchForClientAddress.php?selectType=" + selectType );
    });
});