问题
I am using an ajax call to update a select menu. The ajax call puts the list into my select menu by running from a different .php file.
Here is the JS insertion snippet:
if (req.status == 200) {
document.getElementById('countydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
And here is the HTML:
<label for="county">County:</label>
<div id="countydiv">
<select name="county">
<option>Select State First</option>
</select>
</div>
Easy enough. The script works, but ONLY if the <div id="countydiv">
is in there.
How can I change the JavaScript to still work with markup that looks like this?
<label for="county">County:</label>
<select name="county">
<option>Select State First</option>
</select>
Update: Maybe I ought to include the whole function:
function getCounty(stateId) {
var strURL="findCounty.php?state="+stateId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('countydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
And the markup:
<p>
<label for="state">State:</label>
<select name="state" onChange="getCounty(this.value)">
<option>Select State...</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</p>
<p>
<label for="county">County:</label>
<select name="county">
<option>Select State First</option>
</select>
</p>
回答1:
Try this:
if (req.status == 200) {
var selects = document.getElementsByTagName("select");
for(var i=0;i<selects.length;i++) {
var s = selects[i];
if(s.name == "county") {
var replacement = document.createElement("div");
replacement.innerHTML = req.responseText;
s.parentNode.insertBefore(s,replacement);
s.parentNode.removeNode(s);
break;
}
}
//document.getElementById('countydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
回答2:
Without any parent node, you have to deal with select tag and you cannot use responseText as you wish, Json can help you to fill.
if (req.status == 200) {
var i = 0;
var options = document.getElementById("country").options;
var json = eval("(" + req.responseText + ")");
for (var key in json) {
var value = json[key];
options[i++] = new Option(key, value);
}
}
<label for="county">County:</label>
<select name="county" id="country"></select>
Response from php:
{ "USA": 1, "United Kingdom": 2 }
回答3:
I would suggest going the jquery route to make it easier on yourself:
See this link for something that should lead you in the right direction: How do you remove all the options of a select box and then add one option and select it with jQuery?
Also, there is a bug in IE6 (if you care about supporting it) with dynamically populating Select
elements that you should be aware of. http://support.microsoft.com/kb/276228
来源:https://stackoverflow.com/questions/2304543/how-to-update-select-menu-with-ajax-w-out-div-inside-form