選択メニューを更新するために ajax 呼び出しを使用しています。ajax 呼び出しは、別の .php ファイルから実行することで、リストを選択メニューに入れます。
JS 挿入スニペットは次のとおりです。
if (req.status == 200) {
document.getElementById('countydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
HTML は次のとおりです。
<label for="county">County:</label>
<div id="countydiv">
<select name="county">
<option>Select State First</option>
</select>
</div>
簡単です。スクリプトは機能しますが、そこにある場合のみです<div id="countydiv">
。
このようなマークアップでも機能するように JavaScript を変更するにはどうすればよいですか?
<label for="county">County:</label>
<select name="county">
<option>Select State First</option>
</select>
更新:関数全体を含める必要があるかもしれません:
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);
}
}
そしてマークアップ:
<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>