【问题标题】:Not able to pass value to controller onclick checkbox codeigniter [duplicate]无法将值传递给控制器​​ onclick 复选框 codeigniter [重复]
【发布时间】:2014-01-03 11:47:00
【问题描述】:

我是 CI 新手,我在以下代码中遇到问题:我的控制器从 DB 中动态显示所有国家/地区的值,我需要将点击特定复选框的值传递给我的控制器

我的观点的代码是:

<?php  foreach ( $countryDetails as $row ) {
    echo '<input id="country" type="checkbox" name="country" class="unique" value="'.$row->country_id.'">'.$row->country_name.'<br/>';
}?>
<script>
    $('input.unique').click(function() {
        $('input.unique:checked').not(this).removeAttr('checked');
    });
    $(document).ready(function(){
        $(document).on('click','.unique',function(){
            var id=this.value;
            $.ajax({
                type: "POST",
                context: "application/json",
                data: {id:id},
                url: "http://localhost/index.php/fetch_country/index",
                success: function(msg){
                    alert('$id ');
                }
            });
        });
    });
</script>

控制器

public function index() {
    $this->view_data['countryDetails'] = $this->get_county_model->getCountryDetails();
    $this->load->view('get_country' , $this->view_data );
}
public function fetch() {
    $id=$this->input->post('id');
    echo ('$id');
}

【问题讨论】:

  • 编辑了以下代码!帮我纠正错误。
  • 你现在遇到什么错误?
  • 点击复选框,我没有收到任何警报!没有值被传递。
  • 代替 alert('$id') 试试alert(msg)

标签: php jquery ajax codeigniter codeigniter-2


【解决方案1】:
    $(document).on('click','.unique',function(){
        var id=$(this).val();
        $.ajax({
            type: "POST",
            context: "application/json",
            data: {id:id},
            url: "http://localhost/index.php/fetch_country/index",
            success: function(msg){
                alert(msg);
            }
        });
    });

public function fetch() {
    $id=$this->input->post('id');
    echo $id;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 2017-01-18
    • 2012-12-31
    相关资源
    最近更新 更多