2012-10-29 57 views
0

我想了由线创建JSON选项列表重置每次新的价值观在新选择选项:创建每次

var o=document.createElement("option"); 
document.getElementById("shipTo").options.add(o); 
o.text= address[i]; 

但是,新的选项值被添加到列表中我要在以前没有选项:

var counter; 
function GetAddress() { 
    var customerId = document.getElementById("invoiceTo").value; 
    var xml; 
    if (window.XMLHttpRequest) { 
     xml = new XMLHttpRequest(); 
    } else { 
     xml = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xml.onreadystatechange = function() { 
     if (xml.readyState == 4 && xml.status == 200) { 
      var address = JSON.parse(xml.responseText); 
      document.getElementById("billingAddress").value = address[0]; 
      document.getElementById("shipAddress").value = address[0]; 
      if (counter == 1) { { 
        for (var i = 0; i < address.length; i++) { 
         var removeMe = document.getElementById(o[i]); 
         removeMe.parentNode.removeChild(removeMe); 
         var o = document.createElement("option"); 
         document.getElementById("shipTo").options.add(o); 
         o.text = address[i]; 
        } 
       } 
      } else if (counter != 1) { 
       for (var i = 0; i < address.length; i++) { 
        var o = document.createElement("option"); 
        document.getElementById("shipTo").options.add(o); 
        o.text = address[i]; 
       } 
      } 
      counter = 1; 
     } 
     alert(counter); 
    } 
    xml.open("GET", "GenerateInvoice?customerId=" + customerId, true); 
    xml.send(); 
} 
+0

要替换与您XHR返回的新选项所有现有的选项? –

回答

1

您需要先清除选项:

var oDDL = document.getElementById("shipTo"); 
while (oDDL.options.length > 0) 
    oDDL.options.remove(0); 
+0

thnx ...的想法,但它不工作...显示选择选项列表与旧记录 –

+0

你应该把这个代码之前填充下拉。 –

+0

Thnx很多..代码工作...非常感谢你 –

0

如果我理解正确的话,你婉在添加新选项之前,清除select元素中的所有旧选项。

只需设置options.length = 0;

例如:

var o = document.createElement("option"); 
theSelect.options.length = 0; 
o.text = '400';      
theSelect.options.add(o); 

http://jsfiddle.net/asaf/77sqX/4/

+0

thnx ...的想法,但它不工作...显示选择选项列表与旧记录 –

+0

thnx很多代码工作..非常感谢你 –

0
function GetAddress() 
{ 
var customerId = document.getElementById("invoiceTo").value; 
var xml; 
if (window.XMLHttpRequest) 
{ 
xml=new XMLHttpRequest(); 
} 
else 
{ 
xml=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xml.onreadystatechange= function() 
{ 
if(xml.readyState==4 && xml.status==200) 
{ 
var address= JSON.parse(xml.responseText); 
document.getElementById("billingAddress").value=address[0]; 
document.getElementById("shipAddress").value= address[0]; 
var oDDL = document.getElementById("shipTo");//ship to id of the select in jsp 
while (oDDL.options.length > 0) 
{ 
oDDL.options.remove(0);//clears selects option each time 
} 
for(var i=0;i<address.length;i++) 
{ 
var o = document.createElement("option");//creates optin 
document.getElementById("shipTo").options.add(o);//adds option in the list 
o.text= address[i];//inserts text in the option 
} 
} 
} 
xml.open("GET","GenerateInvoice?customerId="+customerId,true); 
xml.send(); 
} 
+0

这是最终的代码,使用该解决方案后工作正常提供 –

相关问题