2014-10-30 27 views
0

我有一個在函數內動態創建的下拉列表。如何在動態創建表格後保留下拉選擇-jquery

function userlist() { 
$('#user-list').append('<select id="users-type-customers">'+ 
         '<option value="some">Some Users</option>'+ 
         '<option value="AllCustomer">All Users</option>></select>'); 
} 

和我有一個功能,以降低處理上的變化事件下降:

&('#users-type-customers').on("change", function(){ 
    dropdownSelect = $('#users-type-customers :selected').val(); 
    switch (dropdownSelect) { 
      case "AllCustomer": { 
       typeVal = "all"; 
       break; 
      } 
      case "some": { 
       typeVal = "some"; 
       break; 
      } 
     } 
    $.ajax({ 
     type: 'GET', 
     data: {"item": typeVal}, 
     success: function (model) { 
      this.userlist() //calling userlist function 
     } 

    }); 
}) 

所以現在發生的事情是,當我選擇All Users選項,它給了我成功格蘭必要的數據,但現在當我撥打userlist()時,它會將下拉列表附加到表格中並顯示默認值(在這種情況下爲「某些用戶」),這似乎很明顯。我不知道如何調用函數,同時注意保持選擇不變,而不是恢復到默認值。 任何想法?? 謝謝!

回答

0

用途:

function userlist(typeVal) { 
$('#user-list').append('<select id="users-type-customers">'+ 
        '<option value="some">Some Users</option>'+ 
        '<option value="AllCustomer">All Users</option>></select>'); 
$('#users-type-customers').val(typeVal); 
} 
+0

感謝,工作! – user1234 2014-10-30 06:54:36

相關問題