我无法以某种方式使用AJAX更改我的DropDown选项,但是我可以使用我当前的模型填充表单初始化但“OnChange”下拉列表只是重置为NULL而不是填充DropDown字段。
这是我的FORM VIEW的一部分......
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('expenses_exp_by').':', 'exp_by_id',array('class'=>'required wide')); ?>
<div class='form_field'>
<?php echo form_dropdown('exp_by_id', $company, $selected_company, 'id="exp_by_id"');?>
</div>
</div>
<div class="field_row clearfix">
<?php echo form_label($this->lang->line('expenses_product').':', 'prod_id',array('class'=>'required wide')); ?>
<div class='form_field'>
<?php //echo form_dropdown('prod_id', $product, $selected_product, 'id="prod_id"');?>
<?php echo form_dropdown('prod_id', $product, '', 'id="prod_id"');?>
</div>
</div>
<script>
$('#exp_by_id').change(function(){
$("#prod_id > option").remove();
var exp_by_id = $('#exp_by_id').val();
$.ajax({
type: "POST",
url: "expenses/get_products_by_client_ajax/"+exp_by_id,
success: function(prod_id) {
$.each(prod_id,function(prod_id,prod_name)
{
var opt = $('<option />');
opt.val(prod_id);
opt.text(prod_name);
$('#prod_id').append(opt);
});
}
});
});
</script>
这是我的控制器
function get_products_by_client_ajax($exp_by_id = -1){
//$exp_by_id = $this->Expense->get_info($exp_id)->exp_by_id;
foreach($this->Expense->get_products_by_client($exp_by_id)->result_array() as $row)
{
$prod_name[$row['prod_id']] = $row['prod_name'];
}
return $prod_name;
}
这是我的模特
function get_products_by_client($exp_by_id)
{
$query = $this->db->query("
SELECT prod_id, prod_name
FROM products
WHERE client_id = ".$exp_by_id);
return $query;
}
答案 0 :(得分:1)
你的ajax请求不需要发布,它可以是GET。此外,您应指定预期的数据类型。此外,您在两个不同的上下文中使用prod_id
,这会让人感到困惑和容易出错。
试试这个ajax电话:
$.ajax({
type: "GET",
dataType: "json",
url: "expenses/get_products_by_client_ajax/"+exp_by_id,
success: function(products) {
$.each(products,function(prod_id,prod_name)
{
var opt = $('<option />');
opt.val(prod_id);
opt.text(prod_name);
$('#prod_id').append(opt);
});
}
});
在你的控制器中,你想对你的输出进行json编码:
return json_encode($prod_name);