每次都创建新的选择选项

时间:2012-10-29 12:55:39

标签: javascript json

我希望每次在行创建的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();
}

3 个答案:

答案 0 :(得分:1)

您需要先清除选项:

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

答案 1 :(得分: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/

答案 2 :(得分: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();
}