有人可以查看我的代码,看看是什么导致第二次下拉不填充?我不太清楚为什么。先感谢您。
查看:
<div id="finder">
<div class="form">
<select id="brand" name="brand" onchange="myFunction()" >
<option value=""> - Printer Brand - </option>
<?php foreach ($categories as $category_1) { ?>
<?php if ($category_1['category_id'] == $category_id) { ?>
<option value="<?php echo $category_1['category_id']; ?>" selected="selected"><?php echo $category_1['name']; ?></option>
<?php } else { ?>
<option value="<?php echo $category_1['category_id']; ?>"><?php echo $category_1['name']; ?></option>
<?php } ?>
<?php } ?>
</select>
</div>
<br>
<div class="form">
<select name="printerSeries" id="printerSeries">
<option value=""> - Printer Series - </option>
</select>
</div>
<br>
Javascript ajax call
<script type="text/javascript">
function myFunction(){
var brand_id = document.getElementById("brand").value;
$.ajax({
url: 'index.php?route=common/home/getSeries&brand_id=' + brand_id,
type: 'post',
dataType: 'json',
success: function(json) {
$('#printerSeries').append('<option value="<?php echo $json['category_id']; ?>" Selected="selected"><?php echo $json['name']; ?></option>');
}
error: function(){
alert('error');
}
});
}
</script>
这是接收json请求的控制器函数
public function getSeries($brand){
$brand_id = $_POST['brand_id'];
$json = array();
$categories_2 = $this->model_catalog_category->gettoner($brand_id);
foreach ($categories_2 as $category_2) {
$json = array(
'category_id' => $category_2['category_id'],
'name' => $category_2['name'],
);
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
答案 0 :(得分:0)
此问题可能由以下功能引起:$ this-&gt; model_catalog_category-&gt; gettoner($ brand_id);
检查函数是否在每种情况下都返回一个值。如果没有,只需返回任何值。您的下拉菜单将会出现。