How to update Select menu with AJAX w/out <div> inside form

筅森魡賤 提交于 2020-01-03 17:27:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!