简体   繁体   English

复选框切换 select 全部/取消选择所有问题

[英]checkbox toggle select all/unselect all problem

hi i found a script which uses checkbox to select all/ unselect all checkboxex which i found a working example here http://jsfiddle.net/ThiefMaster/HuM4Q/嗨,我找到了一个脚本,它使用复选框到 select 全部/取消选择所有复选框,我在这里找到了一个工作示例http://jsfiddle.net/ThiefMaster/HuM4Q/

the problem is when i tried to integrate it to my own html.问题是当我试图将它集成到我自己的 html 时。 it doenst work anymore, here is my code:它不再起作用了,这是我的代码:

javascript: javascript:

<head>
<script>
$('#checkAll').click(function() {
    if(this.checked) {
        $('input:checkbox').attr('checked', true);
    }
    else {
        $('input:checkbox').removeAttr('checked');
    }
});

$('input:checkbox:not(#checkAll)').click(function() {
    if(!this.checked) {
        $('#checkAll').removeAttr('checked');
    }
    else {
        var numChecked = $('input:checkbox:checked:not(#checkAll)').length;
        var numTotal = $('input:checkbox:not(#checkAll)').length;
        if(numTotal == numChecked) {
            $('#checkAll').attr('checked', true);
        }
    }
});
</script>
</head>

and these is my html:这些是我的 html:

<h2>Courses</h2>
<?php 
$attributes = array('name' => 'list','id' => 'form' );
echo form_open('courses/edit',$attributes);?>

<table class="data display datatable" id="example">
    <thead>
        <tr>
          <th>ID</th>
          <th>Title</th>
          <th>Description</th>
          <th>Units</th>
          <th>Notes</th>
          <th><input type="checkbox" id="checkAll"></th>
        </tr>
    </thead>
    <tbody> 
<?php foreach($query->result_array() as $row): ?>
        <tr class="even gradeC">
            <td><?php echo anchor('courses/details/'.$row['courseID'],$row['courseID']);?></td>
            <td><?php echo $row['courseTitle'];?></td>
            <td><?php echo $row['courseDesc'];?></td>
            <td><?php echo $row['courseUnits'];?></td>
            <td><?php echo $row['courseNotes'];?></td>
            <td><input type="checkbox" name="checkID[]" id="checkID" value="<?php echo $row['courseID'];?>"/></td>
        </tr>
<? endforeach; ?>
    </tbody>
</table>

Try fixing this line:尝试修复此行:

<td><input type="checkbox" name="checkID[]" id="checkID" value="<?php echo $row['courseID'];?>"/></td>

The id attribute should be unique, eg, only one element should have the id "checkID", not every row. id 属性应该是唯一的,例如,只有一个元素的 id 应该是“checkID”,而不是每一行。 Try removing the attribute altogether or make it unique for each row to see if your javascript sorts itself out.尝试完全删除该属性或使其对每一行都唯一,以查看您的 javascript 是否自行排序。

Also, you need a line like this at the top, in your head:此外,您需要在头顶有这样一行:

  <script type='text/javascript' src='http://code.jquery.com/jquery-1.4.4.min.js'></script> 

That script used jQuery.该脚本使用 jQuery。 Make sure you include the jQuery script in your page.确保在页面中包含 jQuery 脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM