【发布时间】:2013-07-10 20:55:18
【问题描述】:
我遇到了一个奇怪的问题。我有一个带有复选框列的表格。我正在使用 JQuery Mobile 复选框。我希望单击一行与单击该行中的复选框相关联。
JS:
$(document).on('pagecreate', function (event) {
bindRowClick();
});
function bindRowClick() {
$('.productTable').find('tr').click(function () {
var chck = $(this).find('td.hyperLink').find('input[type=checkbox]:first');
var chckStatus = chck.is(':checked');
alert("chckStatus 1: " + chckStatus);
alert("!chckStatus: " + !chckStatus);
chck.attr('checked', !chckStatus).checkboxradio().checkboxradio('refresh');
alert("chckStatus 2: " + chck.is(':checked'));
});
}
HTML:这是在<td class="hyperlink">
<fieldset data-role="controlgroup" id="divCheckbox">
<asp:CheckBox runat="server" ID="checkboxSelect" CssClass="custom chckSelect"
meta:resourcekey="checkboxSelectResource1" />
<asp:Label ID="lblForChck" CssClass="lblForChck" AssociatedControlID="checkboxSelect" runat="server"
meta:resourcekey="lblForChckResource1" />
</fieldset>
当我点击第 1 行时发生了什么(直到现在才选中复选框):
first alert returns: "chckStatus 1: false"
second alert returns: "!chckStatus: true"
third alert returns: "chckStatus 2: true"
当我第二次点击 SAME 行时发生了什么(之前选中了复选框):
first alert returns: "chckStatus 1: true"
second alert returns: "!chckStatus: false"
third alert returns: "chckStatus 2: false"
现在是奇怪的部分
当我再次点击同一行时 - 而不是选择复选框(就像第一次一样),我得到了以下结果:
first alert returns: "chckStatus 1: true"
second alert returns: "!chckStatus: false"
third alert returns: "chckStatus 2: false"
我发现可能与此问题有关的内容: Previously working ".checkboxradio('refresh')" now broken in a3
【问题讨论】:
-
为什么要将
bindRowClick();绑定到pagecreate?另外,要刷新复选框,您只需要.checkboxradio('refresh')not.checkbox().checkboxradio('refresh')。 -
pageinit和 pagecreate` 产生相同的结果。关于使用.checkboxradio().checkboxradio('refresh');而不是.checkboxradio('refresh');的原因是,如果您只使用.checkboxradio('refresh');,则会出现错误“未捕获的无法在初始化之前调用checkboxradio 上的方法;尝试调用方法'刷新'” -
要解决这个问题,检查这个答案,这是一个时间问题stackoverflow.com/a/17009087/1771795另一个问题,你最好使用
.prop('checked', true)而不是.attr('checked', true) -
哇 ... .prop 正在工作。非常感谢!!您能否添加您的评论作为答案。 :)
-
我现在也将函数绑定到
pageinit。
标签: jquery jquery-mobile checkbox refresh