I use jquery-1.9.1.js In my html page, it works well for the first time.
just like http://jsfiddle.net/pzCcE/1/
Can somebody help me to improve it?
Change
$(this).attr("checked", true);
to
$(this).prop("checked", true);
jsFiddle example
I actually just answered another question that was similar to this. Per the .prop() docs:
The .prop() method is a convenient way to set the value of properties—especially when setting multiple properties, using values returned by a function, or setting values on multiple elements at once. It should be used when setting selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, or defaultSelected. Since jQuery 1.6, these properties can no longer be set with the .attr() method. They do not have corresponding attributes and are only properties.
Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute. Examples include the value property of input elements, the disabled property of inputs and buttons, or the checked property of a checkbox. The .prop() method should be used to set disabled and checked instead of the .attr() method. The .val() method should be used for getting and setting value.
With answer of j08691 look into following thing also..
It totally makes no sense to create a selector like #id #id because an ID has to be unique within your DOM by definition.
<input type="checkbox" name="checkAll" class="checkAll">
$("#tab1 .checkAll").click(function () {
$("input[name='select_all_photos']").click(function () {
var checked = $(this).is(':checked');
$("input[name^='delete_']").each(function () {
$(this).prop("checked", checked);
});
});
Don't bother doing an if statement.
Add this too....FOR..... if one checkbox (id:book) is unselected means it will cause the main check box (id:checkAll) to be unselected
$("#tab1 #book").click(function () {
if($('#book').is(':checked'))
{
$("#checkAll").prop("checked", false);
}
});
You should be using classes with the same name, ID's MUST be unique!
<input type="checkbox" name="checkAll" id="checkAll">全選
<input type="checkbox" name="book" class="book" value="book1">book1
<input type="checkbox" name="book" class="book" value="book2">book2
<input type="checkbox" name="book" class="book" value="book3">book3
<input type="checkbox" name="book" class="book" value="book4">book4
<input type="checkbox" name="book" class="book" value="book5">book5</table>
$(function () {
$("#checkAll").click(function () {
if ($("#checkAll").is(':checked')) {
$(".book").prop("checked", true);
} else {
$(".book").prop("checked", false);
}
});
});
I've just simplified @j08691 answer
$("#checkAll").click(function() {
var allChecked = $(this);
$("#tab1 input[type=checkbox]").each(function() {
$(this).prop("checked", allChecked.is(':checked'));
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tab1">
<tr>
<td>
<input type="checkbox" name="checkAll" id="checkAll">Select all
<input type="checkbox" name="book" id="book" value="book1">book1
<input type="checkbox" name="book" id="book" value="book2">book2
<input type="checkbox" name="book" id="book" value="book3">book3
</td>
</tr>
</table>