【问题标题】:Javascript - Changing tag-it source with select optionJavascript - 使用选择选项更改 tag-it 源
【发布时间】:2017-04-11 06:45:14
【问题描述】:

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

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",
                        ..
                    });
                  },                        
                    ...
                })
            });         

    } 

}); 

【问题讨论】:

    标签: javascript jquery tag-it


    【解决方案1】:

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

    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,然后发送到数据库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 2011-04-12
      • 1970-01-01
      相关资源
      最近更新 更多