我想每次在通过以下行创建 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();
}