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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3399139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:29:23  来源:igfitidea点击:

Jquery: Possible to dynamically change source of Autocomplete widget?

javascriptjquery

提问by Ryan

Greetings,

你好,

I am using the official Autocomplete jquery widget and am having troubles dynamically changing a variable (selectType) I'm passing via the query string. The variable would change depending upon which option is selected via a select box.

我正在使用官方的自动完成 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
}); 
});

回答by PetersenDidIt

Try actually changing the sourceoption of the autocomplete on the change event.

尝试实际更改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 );
    });
});