2015-02-09 54 views
1

我想從選項標籤組合框的數據庫中追加值。但是當我第一次選擇標籤的值爲空,然後通過AJAX追加時,它只顯示一個值。但我希望所有選項都可見,但應自動選擇。在jquery mobile中動態選擇組合框

我的代碼

$('#b').on("click", "a", function() { 
    var patid= $(this).attr('id'); 
    $.ajax({ 
    type:"GET", 
    url:"https://localhost/patient_details1.php?patid="+patid, 
    dataType:'JSON', 
    success:function(response) 
    { 
     ("#pat_type").html(""); 
     for (var i=0;i<response.length;i++) 
      { 
      $('<option value="'+ response[i].pattype +'">'+ 
    response[i].pattype +'</option>').appendTo("#pat_type"); 
        } 
      } 
    } 
    });    
}); 

回答

0
  1. 使用$( '#pat_type')。空()。
  2. 檢查您何時從您的請求中獲取數據。只有在加載頁面後,您才需要獲取它 。
0

您有錯字錯誤("#pat_type").html("");

使其$("#pat_type").html("");$('#pat_type').empty();

for (var i = 0; i < response.length; i++){ 
     listItems+= "<option value='" + response[i].pattype + "'>" + response[i].pattype + "</option>"; 
    } 

    $("#pat_type").append(listItems); 

// for setting selected item based on value 
$('#pat_type').val('selectedvalue');