2017-04-11 76 views
0

我有一個選擇兩個選項。當改變選擇...我想過濾/自動完成一個不同的數據庫。沒有http://aehlke.github.io/tag-it/它工作正常,自動完成正在改變源...但不是與它。它停留在source_01.php,儘管我在控制檯中看到測試源01改變爲測試源02.什麼可能導致這種情況?Javascript - 更改標籤來源與選擇選項

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

    } 

}); 

回答

0

您的問題是由腳本沒有得到回升,當您更改的選項,如果你想選擇的改變選項的值值你必須做類似於下面的代碼。

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然後到數據庫中的隱藏文字。