使用 JavaScript 检查和取消选中转发器控件中的复选框?
如何使用 javascript 选中和取消选中转发器控件中的复选框? 在这里,我无法在单击中选中复选框或在单击中取消选中复选框。
我的代码是:
<asp:Repeater id="repeaterentry" runat="server" >
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th style="width:10px" align="left"><asp:CheckBox ID="allCheckbox1" runat="server" /></th>
<th><asp:LinkButton ID="lnkbtn1" runat="server" CommandName="UserName">Name</asp:LinkButton></th>
<th>Password</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width:10px"><asp:CheckBox ID="chkContainer" runat="server" /></td>
<td><%#Eval("uname") %> </td>
<td><%#Eval("upass")%> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
jquery:
<script type="text/jscript" language="jscript">
window.onload = function () {
var $allCheckbox = $('<%= this.repeaterentry.ClientID %>'); // you could use a class here either - depends on you having access to '<%= this.allCheckbox.ClientID %>'
$allCheckbox.change(function () {
var $table = $allCheckbox.closest('table');
var $checkboxes = $(':checkbox', $table).not($allCheckbox); // this selector is a bit evil, if you have other checkboxes in the table as well ...
if ($allCheckbox.is(':checked')) {
$checkboxes.attr('checked', 'checked');
}
else {
$checkboxes.removeAttr('checked');
}
});
}
}
</script>
how to check and uncheck the checkbox in repeater control using javascript?
here i m unable to check the checkbox in single click or uncheck the checkbox in single check.
my code is:
<asp:Repeater id="repeaterentry" runat="server" >
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th style="width:10px" align="left"><asp:CheckBox ID="allCheckbox1" runat="server" /></th>
<th><asp:LinkButton ID="lnkbtn1" runat="server" CommandName="UserName">Name</asp:LinkButton></th>
<th>Password</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width:10px"><asp:CheckBox ID="chkContainer" runat="server" /></td>
<td><%#Eval("uname") %> </td>
<td><%#Eval("upass")%> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
the jquery :
<script type="text/jscript" language="jscript">
window.onload = function () {
var $allCheckbox = $('<%= this.repeaterentry.ClientID %>'); // you could use a class here either - depends on you having access to '<%= this.allCheckbox.ClientID %>'
$allCheckbox.change(function () {
var $table = $allCheckbox.closest('table');
var $checkboxes = $(':checkbox', $table).not($allCheckbox); // this selector is a bit evil, if you have other checkboxes in the table as well ...
if ($allCheckbox.is(':checked')) {
$checkboxes.attr('checked', 'checked');
}
else {
$checkboxes.removeAttr('checked');
}
});
}
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更新我对
工作示例的答案可用
updating my answer to
working example available
访问此我发布了一种更简单的方法来检查和取消选中复选框。
https://stackoverflow.com/a/8779907/1054978
Visit this i have posted an easier method to check and uncheck the checkboxes.
https://stackoverflow.com/a/8779907/1054978
最后我得到了输出:
Finally I got the Output:
对 Andreas Niederair 答案的一点贡献。为了使其能够与 jQuery 1.9+(或者我已经检查过)一起使用,请将
removeAttr('checked');
替换为prop('checked',false);
和 < code>attr('checked', 'checked'); 和prop('checked', 'checked');
我花了一些时间才解决这个问题!
A small contribution to Andreas Niedermair answer. For this to work with jQuery 1.9+ (or so I have checked) replace the
removeAttr('checked');
withprop('checked',false);
andattr('checked', 'checked');
withprop('checked', 'checked');
It took me some time to work it out!
这是我的要求的最终解决方案。这里我使用 div 因为当您获取视图源时,转发器 id 不会生成 html 页面。所以我从 div 中获取了 id 并将其应用到 javascript 中。
This is the Final Solution for My Requirement.Here i m using div because when you take the view source the repeater id is not generated html page. so for i taken the id from div and apply into the javascript.