Javascript 为什么我的checkall功能不起作用?

Javascript 为什么我的checkall功能不起作用?,javascript,php,jquery,Javascript,Php,Jquery,我正在使用php、jquery和html处理MVC。在我的视图页面中,我有一个调用函数的代码,该函数根据某些参数创建一个复选框列表,还有一个javascript函数,该函数在调用该函数和用户选中“全部”按钮时检查所有复选框。 下面的部分使用id或ward调用函数 <?=form_label('Wards : ')?> <span id="wards"></span> 这是生成复选框的Ward函数 public function wards(){

我正在使用php、jquery和html处理MVC。在我的视图页面中,我有一个调用函数的代码,该函数根据某些参数创建一个复选框列表,还有一个javascript函数,该函数在调用该函数和用户选中“全部”按钮时检查所有复选框。 下面的部分使用id或ward调用函数

 <?=form_label('Wards  : ')?> <span id="wards"></span>
这是生成复选框的Ward函数

public function wards(){
        $id = $this->input->post('id');

        $this->db->join('building', 'building.id = ward.building_id');
        $this->db->select('ward.*, building.name as building_name');
        $this->db->where_in('ward.building_id', explode(',',$id));
        $sql = $this->db->get('ward');  
        $wards = $sql->result();

        $editid = $this->uri->segment(3);

        $this->db->select('wards');
        $userwards = $this->db->get_where('users', array('id'=>$editid))->result();

        $userwards = explode(',', $userwards[0]->wards);
        $data = array('name'=> 'wards[]', 'id'=> 'checkall', 'value' => 'All Observations','style'  => 'float: left; margin-right: 10px'); 
        if(!empty($wards)){
        echo form_checkbox($data).' '.form_label('All Wards');
            foreach($wards as $w){
                if(in_array($w->id, $userwards)){
                    $checked = 'TRUE';  
                } else { $checked = ''; }
                $warddata = array('name'=> 'wards[]', 'value' => $w->id, 'class' => 'w', 'checked'=> $checked, 'style'  => 'float: left; margin-right: 10px'); 
                echo                
                form_checkbox($warddata).' '.form_label($w->name.' ('.$w->building_name.')');   
            }
        }
        else { echo '<font color="#c00">No Wards are assigned to this building!</font>';}

    }
公共功能病房(){
$id=$this->input->post('id');
$this->db->join('building','building.id=ward.building_id');
$this->db->select('ward.*,building.name作为building_name');
$this->db->where_in('ward.building_id',explode(',',$id));
$sql=$this->db->get('ward');
$wards=$sql->result();
$editid=$this->uri->segment(3);
$this->db->select('wards');
$userwards=$this->db->get_where('users',array('id'=>$editid))->result();
$userwards=explode(“,”,$userwards[0]->wards);
$data=array('name'=>'wards[]','id'=>'checkall','value'=>'All Observations','style'=>'float:left;margin right:10px');
如果(!空($wards)){
回显表格复选框($data)。“”。表格标签('All WARD');
foreach($w){
if(在数组中($w->id,$userwards)){
$checked='TRUE';
}else{$checked='';}
$warddata=array('name'=>'wards[]','value'=>$w->id,'class'=>'w','checked'=>$checked','style'=>'float:left;margin right:10px');
回音
form_复选框($WARDATA)。“”。form_标签($w->name.”(“.$w->building_name.”);
}
}
否则{echo'此建筑未分配病房!';}
}

因此,复选框按预期生成,但当我选中“所有病房”复选框时,其他复选框不会被选中。我做错了什么?

以防万一您犯了最常见的jQuery错误:

您是否将jQuery包装在文档加载/就绪事件中

旧语法是
$(document).ready(function(){…})$(function(){…})

e、 g

否则,请检查您的代码是否使用类似于
警报的简单方式触发:

e、 g

如果所有这些都失败了,请尝试在打开F12调试窗口的情况下在Google Chrome中查看您的页面

它将列出所有发现的Javascript/jQuery错误,您可以中断代码、查看变量内容等


由于PHP基本上是一种只写语言,类似这样的问题也需要显示它生成的HTML输出,最好是在jQuery的JSFIDLE中。

我们不需要您的PHP代码,请向我们展示您呈现的HTML。理想情况下,将所有这些也放在JSFIDLE中。您的jQuery代码是否真的启动了?是否将其包装在文档加载事件中?顺便说一句,您可以将jQuery代码缩短为:
$('checkall').change(function(){$('input.w:checkbox”).prop('checked',$(this.prop('checked');})--
public function wards(){
        $id = $this->input->post('id');

        $this->db->join('building', 'building.id = ward.building_id');
        $this->db->select('ward.*, building.name as building_name');
        $this->db->where_in('ward.building_id', explode(',',$id));
        $sql = $this->db->get('ward');  
        $wards = $sql->result();

        $editid = $this->uri->segment(3);

        $this->db->select('wards');
        $userwards = $this->db->get_where('users', array('id'=>$editid))->result();

        $userwards = explode(',', $userwards[0]->wards);
        $data = array('name'=> 'wards[]', 'id'=> 'checkall', 'value' => 'All Observations','style'  => 'float: left; margin-right: 10px'); 
        if(!empty($wards)){
        echo form_checkbox($data).' '.form_label('All Wards');
            foreach($wards as $w){
                if(in_array($w->id, $userwards)){
                    $checked = 'TRUE';  
                } else { $checked = ''; }
                $warddata = array('name'=> 'wards[]', 'value' => $w->id, 'class' => 'w', 'checked'=> $checked, 'style'  => 'float: left; margin-right: 10px'); 
                echo                
                form_checkbox($warddata).' '.form_label($w->name.' ('.$w->building_name.')');   
            }
        }
        else { echo '<font color="#c00">No Wards are assigned to this building!</font>';}

    }
$(function(){
    $('#checkall').change(function(){

        if($(this).prop('checked')){
         //uncheck all the checkboxes   
         $("input.w:checkbox").each( 
           function() { 
              $(this).prop('checked',true);
           } 
         );
       }else{
          //check all the checkboxes  
          $("input.w:checkbox").each( 
           function() { 
              $(this).prop('checked',false);
           } 
         ); 
       }
    });
});
    $('#checkall').change(function(){
        alert("Yes, it fired!");   // Use liberally while developing :)        
        if($(this).prop('checked')){