[英]Dropdown list ajax
有人可以查看我的代碼,看看是什么導致第二個下拉列表無法填充嗎? 我不太清楚為什么。 先感謝您。
視圖:
<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調用
<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));
}
這里的問題可能是由以下功能引起的:$ this-> model_catalog_category-> gettoner($ brand_id);
檢查函數是否在每種情況下都返回一個值。 如果沒有,只需返回任何值。將出現您的下拉菜單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.