从jQuery 1.4.4迁移到jquery 1.6.4开始破坏与radiobuttonlist相关的代码。
以下是复制奇怪行为的示例代码和步骤:
重现的步骤:
1:已选择广播A。
2:选择收音机B
3:在文本框中输入内容并标签输出。
您将看到: A已选中且B已检查警报。
那么究竟从1.6.4变为1.4.4会导致它破裂?
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.6.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
function txtchanged(tb) {
$(".rbl").find("input[type='radio']").each(function () {
alert($(this).val() + " " + $(this).attr("checked"));
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" CssClass="rbl">
<asp:ListItem Text="A" Value="A" Selected="True" />
<asp:ListItem Text="B" Value="B" />
</asp:RadioButtonList>
<asp:TextBox ID="tb" runat="server" CssClass="tb" onblur="txtchanged(this)"></asp:TextBox>
</form>
</body>
</html>
答案 0 :(得分:5)
引用OP:
那导致它从1.6.4到1.4.4究竟发生了什么变化 破?
单选按钮checked
不是属性,而是属性。 jQuery在1.6版中更新以纠正此语义错误。
改变这个......
.attr("checked")
到此......
.prop("checked")
<强>编辑:强>
关于OP关于为什么jQuery不向后兼容的评论:
1)它会在代码中添加膨胀积累。
2)“向后兼容”几乎与完全没有解决此问题相同。
See this page to read the release notes on every version of jQuery
同时改变这个......
.removeAttr('checked')
到此......
.removeProp('checked')