这段代码适用于jQuery,ver.1.6
<input type="checkbox" class="radio" value="1" name="sameName" />
<input type="checkbox" class="radio" value="1" name="sameName" />
<input type="checkbox" class="radio" value="1" name="sameName" />
$("input:checkbox").click(function() {
if ($(this).attr("checked") === true) {
var group = "input:checkbox[name='" + $(this).attr("name") + "']";
$(group).attr("checked", false);
$(this).attr("checked", true);
} else {
$(this).attr("checked", false);
}
});?
Run Code Online (Sandbox Code Playgroud)
但是如果我使用jQuery 1.7的新方法更改代码它不会工作?为什么?谢谢你的时间:
$("input:checkbox").on('click', function() {
if ($(this).attr("checked") === true) {
var group = "input:checkbox[name='" + $(this).attr("name") + "']";
$(group).attr("checked", false);
$(this).attr("checked", true);
} else {
$(this).attr("checked", false);
}
});?
Run Code Online (Sandbox Code Playgroud)
用.prop('checked',true)而不是.attr.
此外,if ($(this).attr("checked") === true)可以简化为简化
if ($(this).prop("checked")) {
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
208 次 |
| 最近记录: |