2016-07-12 60 views
0
function successCallback(caRecords) { 

     var x = document.getElementById("custAccount"); // select 
     var option1 = document.createElement("option"); //options 
     //var accno = 0; 

    // caRecords i am fetch from MS CRM 
     var count = caRecords[0].results.length; 

     if (caRecords != null && count > 0) { 
      alert("records are not null"); 

      for (var i = 0 ; i < count; i++) 
      { 
       var text = caRecords[0].results[i].new_name; 
       // alert(text + "J=" + j); 
       option1.text = text; 
       option1.value = j; 
       x.add(option1); 
       j++; 

      } 
    } 

我得到了六條記錄,並試圖將該值插入select作爲選項。它顯示了我6個值的最後一個值。無法添加多個選項來選擇

enter image description here

誰能幫助我提高我的代碼?

+0

你必須要添加一個新的選項,每次創建一個新元素。因此,不要在循環外部創建元素'option1',而是在循環內部創建它。 – Quirk

+0

感謝您的幫助。它正在工作.. –

回答

0

可以遍歷你的價值觀是這樣的...

function successCallback(caRecords) { 
     var x = document.getElementById("custAccount"); // select 
     var options = ""; 
     var count = caRecords[0].results.length; 
     if (caRecords != null && count > 0) { 
      alert("records are not null"); 
      for (var i = 0; i < count; i++) { 
      options += "<option value=" + j + ">" + caRecords[0].results[i].new_name + "</option>"; 
      j++; 
      } 
      x.innerHTML = options; 
     }