Jquery:可以动态更改自动完成小部件的来源吗?

时间:2010-08-03 17:41:24

标签: javascript 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
}); 
});

1 个答案:

答案 0 :(得分:75)

尝试在更改事件中实际更改自动填充的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 );
    });
});