0
我在項目中使用依賴下拉菜單。我幾次檢查過我的代碼,但仍然出現錯誤。codeigniter依賴下拉,使用xmlhttp時出現問題
數據已加載到'類別'下拉菜單。當Iam從「類別」下面的消息中選擇項目時將顯示。未將數據加載到「子類別」中。
「有一個問題,而使用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);
}
}
}
爲什麼你當你不使用''ajax'加載jQuery'圖沙· – Tushar
先生。請解釋 –
閱讀:http://api.jquery.com/jquery.ajax/ – Tushar