2015-11-02 59 views
1

我有從我的數據庫中獲得的多個依賴下拉菜單。 第一個下拉列表將選擇eOpp,第二個下拉列表將基於從第一個下拉列表中選定的eOpp。一旦選擇了其他值,清除相關下拉菜單

//First Drop down 
<label>Select eOpp</label> 
<?php erfq_generateOppDropdown($oppID,"erfq_rfq_oppID");?> 

//Second Drop down 
<label>Select Item</label> 
<select id="item" name="item[]" multiple="multiple"> 
</select> 

這是我的ajax從第一個下拉菜單中獲得價值。

function getItem(val) { 
    $.ajax({       
     type: "POST", 
     url: "get_item.php", 
     data:'erfq_rfq_oppID='+val, 
     success: function(data){      
      $("#item").empty().html(data);   
      $("#item").multipleSelect("refresh");                
     } 
    }); 
} 

它可以很好地生成這兩個下拉。但是當第一個下拉菜單(選擇eOpp)發生變化時,第二個下拉菜單仍然會保留我下拉菜單中的前一個值。我使用這個multiSelect爲我的第二個下拉菜單下的基礎1。 jquery.multiple.select.js

例如,當我選擇的第一個eOpp,結果會是這樣:

 
Select eOpp: 1 
Select Item: 
Item 1(A) 
Item 1(B) 

但我改變了選擇eOpp後,它會變成這個樣子:

 
Select eOpp: 2 
Select Item: 
Item 1(A) 
Item 1(B) 
Item 2(A) 

它會保留以前的值eOpp = 1但是當我使用php到$_POST它時,我沒有任何價值。我必須相應地刪除了此前的紀錄時,我改變時,多選實現我的選擇eOpp
編輯
出現問題。

$(function() { 
    $('#item').change(function() { 
     console.log($(this).val()); 
    }).multipleSelect({ 
     width: '100%' 
    }); 
}); 
+0

嘗試$(「#item」)。html('')。html(data); – Karan

+0

嗨@Karan,感謝您的回覆,但它不起作用。我已經嘗試過'multiSelect(「」)和'multiSelect(「destroy」)'但仍然無法工作。 – Sollo

+0

是'select' id唯一嗎? – Karan

回答

0

後,我的代碼工作得很好。 欲瞭解更多信息,請參閱JSFiddle。函數的一些變化是:

1) function MultipleSelect($el, options) 


2) MultipleSelect.prototype = { 
    constructor : MultipleSelect, 

    init: function() { 
     var that = this, 
      html = []; 
     if (this.options.filter) { 
      html.push(
       '<div class="item-search">', 
        '<input type="text" autocomplete="off" autocorrect="off" autocapitilize="off" spellcheck="false">', 
       '</div>' 
      ); 
     } 
3) optionToHtml: function (i, elm, group, groupDisabled) { 
0

銷燬和reinitalizing應該工作:我已經改變了多選插件

$("#item").multiselect('destroy'); 
$("#item").multiselect(); 
+0

它仍然是一樣的。如果我銷燬#item,我無法獲取數據 – Sollo

相關問題