我的表单上有2个选择菜单:类型和类别。当用户从“类型”菜单中选择某些内容时,我希望它执行一个PHP脚本,该脚本查询数据库并返回所选类型的匹配类别列表,供他们在“类别”菜单中进行选择。
这很好用,但我现在想更新它,以便它为categoryGroup添加一个类("成功"或"错误"取决于AJAX结果)。这是我的桌子上有2个单元格:
<div class="form-group">
<label for="title" class="control-label col-sm-3">Type</label>
<div class="input-group col-xs-8">
<select class="form-control" name="type" id="type" onchange="getCategories(this.value)">
<option value="" selected></option>
<option value="Business">Business</option>
<option value="Commercial">Commercial</option>
<option value="Commercial Land">Commercial Land</option>
<option value="Land">Land</option>
<option value="Rental">Rental</option>
<option value="Residential">Residential</option>
<option value="Rural">Rural</option>
</select>
</div>
</div>
<div class="form-group" id="categoryGroup">
<label for="title" class="control-label col-sm-3">Category</label>
<div class="input-group col-xs-8" class="" id="categoryList">
<select class="form-control" name="category" id="category">
<option value="" selected></option>
</select>
</div>
</div>
并且我的脚本正在努力用AJAX调用中的相应选项替换Category select菜单:
<script type="text/javascript">
function getCategories(str) {
if (str == "") {
document.getElementById("categoryList").innerHTML = "";
return;
}
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("categoryList").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("POST", "getPropertyCategories.php?type=" + str, true);
xmlhttp.send();
}
</script>
我无法解决如何更新此问题,以便将类添加到categoryGroup id
答案 0 :(得分:0)
非常基本,但我认为你得到了它的要点:
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("categoryList").innerHTML = xmlhttp.responseText;
}
var status = xmlhttp.status == 200 ? 'success' : 'error'
var group = document.getElementById("categoryGroup")
group.classList.add(status)
}