- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用jquery-1.9.1.js在我的 html 页面中,它第一次运行良好。
就像 http://jsfiddle.net/pzCcE/1/
有人可以帮我改进它吗?
<table id="tab1">
<input type="checkbox" name="checkAll" id="checkAll">全選
<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
<input type="checkbox" name="book" id="book" value="book4">book4
<input type="checkbox" name="book" id="book" value="book5">book5</table>
$(function () {
$("#tab1 #checkAll").click(function () {
if ($("#tab1 #checkAll").is(':checked')) {
$("#tab1 input[type=checkbox]").each(function () {
$(this).attr("checked", true);
});
} else {
$("#tab1 input[type=checkbox]").each(function () {
$(this).attr("checked", false);
});
}
});
});
最佳答案
改变
$(this).attr("checked", true);
至
$(this).prop("checked", true);
<强> jsFiddle example
我实际上刚刚回答了另一个与此类似的问题。根据 .prop()文档:
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.
关于jquery - 选中取消选中所有复选框与另一个单个复选框使用jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15504643/
我是一名优秀的程序员,十分优秀!