用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1402|回复: 0
收起左侧

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

[复制链接]
发表于 2014-12-1 14:48:10 | 显示全部楼层 |阅读模式
本帖最后由 fastammo 于 2014-12-3 08:27 编辑

勾选东西印不出来,是哪边写错了吗?
test_model.php
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
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
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[type=checkbox]').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>
 
复制代码


本版积分规则