fastammo 发表于 2014-12-1 14:48:10

AJAX checkbox勾选传值,即时显示问题

本帖最后由 fastammo 于 2014-12-3 08:27 编辑

勾选东西印不出来,是哪边写错了吗?
test_model.php

class test_model extends CI_Model {

    function __construct()
    {
            parent::__construct();
    }
      
      public function add_test($id)
      {
                $query = $this->db->query("SELECT name,size FROM test WHERE id=$id");                        
                return$query->result_array();
      }
}      

test_controllers.php

class test_controllers extends CI_Controller {

      public $quick_menu;

      public function __construct()
      {
                parent::__construct();

                $this->load->model('test_model');
      }
      public function index()
      {
                self::add();
      }      
      public function add()
      {
                $id = $this->input->post('id');      
                $data['test'] = $this->test_model->add_test($id);
                $this->load->view('test_view', $data);
      }
}      

test_view.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script>
$(function(){      
      $('.test_class input').on('change',function(){
                var id = $(this).attr('value');
                var check = ($(this).attr('checked')=='checked')?1:0;
                        if(check==1){
                        add(id); //add item
                }else{
                        del(id); //del item
                }
      });
})
function add(id){
      $.post('/diy/add',{id:id},function(rs){
      });      
}
function del(id){
      $.post('/diy/del',{id:id},function(rs){
      });      
}
</script>
</head>

<body>
<div class="test_class">
<input type="checkbox" id="prod_1" value='1'>西瓜
<input type="checkbox" id="prod_1" value='2'>香蕉
<input type="checkbox" id="prod_1" value='3'>苹果
</div>
<tr><!--勾选后,name,size会印在这边--><tr>
</body>
</html>


页: [1]
查看完整版本: AJAX checkbox勾选传值,即时显示问题