我不能以某种方式使用 AJAX 更改我的下拉选项,但是我可以使用我当前的模型来填充表单初始化但“OnChange”下拉列表只是重置为 NULL 而不是填充下拉字段。
这是我的表单视图的一部分...
<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;
}