2013-08-30 116 views
3
<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#select-dept").change(function() { 
      var id = $("#select-dept").val(); 
      $.ajax({ 
       type: "POST", 
       url: "<?=base_url()?>.index.php/sms/get_dept_employee", 
       //url: baseurl + 'sms/get_dept_employee', 
       data: "id", 
       dataType = "json", 
       cache: "false", 
       success: function (emp_list) { 
        $("#dept-emp").html(emp_list); 
       } 
      }); 
     }); 
    }); 
</script> 

我无法查看数据发送到控制器功能 鉴于他们是从MySQL数据库笨AJAX发送数据使用Ajax代码到控制器

<select class="select-dept" id="select-dept" name="select-dept"> 
    <option>Select Department</option> 
    <?foreach ($departments as $dt):?> 
     <option value="<?=$dt['id']?>"> 
      <?=$dt[ 'name']?> 
     </option> 
    <?endforeach;?> 
</select> 

我需要得到刷新departmenr值选择框当用户选择部门和需要调用控制器功能 get_dept_employee 需要显示员工列表的数据网格

回答

13

您需要发送的数据选择,因为对象

试试这个

..... 
url: "<?=base_url()?>.index.php/sms/get_dept_employee", 
data: {"id":id}, 
dataType:"json", 
.... 

,并获得价值贴在你的控制器id ..

$id=$this->input->post('id'); 
.... 
2
var id = $("#select-dept").val();  

data:"id", //Here you are sending string as id 

应该

 data:id, //No double quotes surrounded 
0

尝试:

data: {'id':$(this).val()}, 
0

我看到了一些错误

使用data: {'id':id},代替data: "id",

使用dataType:"json",代替dataType="json",

使用cache:false代替cache: "false",

<script type="text/javascript"> 
    $(document).ready(function() { 

     var base_url = "<?php echo $this->config->item('base_url'); ?>"; 

     $("#select-dept").change(function() { 

      var id = $(this).val(); 
      $.ajax({ 
       type: "POST", 
       url: base_url+"index.php/sms/get_dept_employee"; 
       data: {'id':id}, 
       dataType:"json", 
       cache: false 
       success: function (emp_list) { 
        $("#dept-emp").html(emp_list); 
       } 
      }); 
     }); 
    }); 
</script> 
相关问题