我在项目中使用依赖下拉列表。我已经多次检查过我的代码,但仍然有错误。
数据已加载到“类别”下拉菜单。当Iam从'Category'中选择项目时,将显示以下消息。没有数据加载到“子类别”。
“使用XMLHTTP时出现问题”
请查看我的代码
<!-- /.Category -->
<div class="form-group">
<label>Product Category</label>
<select name="category_id" class="form-control col-sm-5" id="category" onchange="get_category(this.value)">
<option value="">Select Product Category</option>
<?php if (!empty($category)): ?>
<?php foreach ($category as $v_category) : ?>
<option value="<?php echo $v_category->category_id; ?>"
<?php
if (!empty($product_info)) {
echo $v_category->category_id == $product_category->category_id ? 'selected' : '';
}
?> >
<?php echo $v_category->category_name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>
</div>
<!-- /.Sub Category -->
<div class="form-group">
<label>Subcategory<span class="required">*</span></label>
<select name="subcategory_id" class="form-control col-sm-5" id="subcategory">
<option value="">Product Subcategory</option>
<?php if (!empty($subcategory)): ?>
<?php foreach ($subcategory as $v_subcategogy) : ?>
<option value="<?php echo $v_subcategogy->subcategory_id; ?>"
<?php
if (!empty($product_info)) {
echo $v_subcategogy->subcategory_id == $product_info->subcategory_id ? 'selected' : '';
}
?> >
<?php echo $v_subcategogy->subcategory_name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>
</div>
AJAX代码(我认为响应中的问题( req.status == 200 )部分。)
//*********************************************
// Product Category to Subcategory
//*********************************************
function get_category(str) {
if (str == '') {
$("#subcategory").html("<option value=''>Select Subcategory</option>");
} else {
$("#subcategory").html("<option value=''>Select Subcategory</option>");
var link = getBaseURL();
var strURL = link + "admin/product/get_subcategory_by_category/" + str;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
var result = req.responseText;
//alert(result);
$("#subcategory").html("<option value=''>Select Subcategory</option>");
$("#subcategory").append(result);
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("POST", strURL, true);
req.send(null);
}
}
}