Javascript - 使用 select 选项更改 tag-it 源

Javascript - Changing tag-it source with select option

我有一个 select 有两个选项。更改 select 时...我想 filter/autocomplete 一个不同的数据库。没有 http://aehlke.github.io/tag-it/ 它工作正常并且自动完成正在更改源......但不是它。它停留在 source_01.php,尽管我在控制台中看到测试源 01 更改为测试源 02。这可能是什么原因造成的?

HTML:

<select id="search_database" class="form-control">
    <option value="1" selected="">Source 01</option>
    <option value="2">Source 02</option>
</select>

Javascript:

$('#search_database').on('change', function () {
    if ( $('#search_database').val() == 1 ) { 
        console.log('Test Source 01');          


        $("#input-newsearch").tagit({
            ...
            autocomplete: ({
                  source: function( request, response ) {
                    ...
                    $.ajax({
                        url: "/source_01.php",
                        ...
                    });
                  },                        
                    ... 
                })

            });


    } else if ( $('#search_database').val() == 2 ) {
        console.log('Test Source 02');          

        $("#input-newsearch").tagit({
            ...
            autocomplete: ({
                  source: function( request, response ) {
                    ...
                    $.ajax({
                        url: "/source_02.php",
                        ..
                    });
                  },                        
                    ...
                })
            });         

    } 

}); 

你的问题是当你更改选项时脚本没有获取值如果你想选择更改选项的值,你必须执行类似于以下代码的操作。

HTML:

<label for="clientSelect" style="margin-left:14px;">Client:</label>
   <select name="clientSelect" id="clientSelect" onChange="clientId(this.id)" style="width:180px;"></select>
   <input type="text" id="clintId" size="1" hidden>

JavaScript:

function getClient(cId){
    //Get the selected ID using this.is in client side HTML then breaks it up using this to get the ID only
          var select = document.getElementById("catSelect"),
          optionId = select.options[select.selectedIndex],
          catId = optionId.id;

我使用隐藏文本字段将选定的 ID 发送到我的 PHP,然后再发送到数据库。