jQuery関数を使用して、チェックボックスの.attr()
チェックとチェックを外しています。最初は機能しますが、それ以上は機能しません。
それに関するいくつかの質問を読んだ後、私はこれを機能させることができません。例これとこれとこれ
Chrome と Firefox でテストしました。jsFiddleを使用しても同じ問題が発生します。
問題はどこだ?JavaScript コード、HTML、またはどこで?
これはすべてのコードです:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Searchs</title>
</head>
<body>
<div id="results-body">
<table>
<thead>
<tr>
<td class="small center">
<input type="checkbox" id="checkall" onclick="master();" />
</td>
</tr>
</thead>
<tbody>
<tr>
<td class="center">
<input type="checkbox" class="slave" onclick="slave();" />
</td>
</tr><tr>
<td class="center">
<input type="checkbox" class="slave" onclick="slave();" />
</td>
</tr>
</tbody>
</table>
</div>
<!-- LOAD THE JS AT THE END OF THE FILE -->
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
master = function() {
if ($( '#checkall:checkbox' ).is(":checked"))
$('input:checkbox').attr('checked',true);
else
$('input:checkbox').attr('checked',false);
}
slave = function() {
var allSelected = true;
$( '.slave[type="checkbox"]' ).each(function() {
if ($( this ).is(":not(:checked)")){
allSelected = false;
//return;
}
});
if (!allSelected) {
//$( '#checkall:checkbox' ).removeAttr('checked');
alert("REMOVE: \nFrom " + $( '#checkall:checkbox' ).attr('checked') );
$( '#checkall:checkbox' ).attr('checked',false);
alert("to " + $( '#checkall:checkbox' ).attr('checked') );
} else {
alert("ADD: \nFrom " + $( '#checkall:checkbox' ).attr('checked') );
$( '#checkall:checkbox' ).attr('checked',true);
alert("to " + $( '#checkall:checkbox' ).attr('checked') );
}
}
});
</script>
</body>
</html>