The first column in my gridview (gvAvailable) is a currently checkbox column "chkSelect". The way it works now is that a user can check multiple checkboxes on a gridview, but I would like a jquery function to deselect all the checkboxes on the gridview except for the checkbox that was clicked.
gridview中的第一列(gvAvailable)是当前复选框列“chkSelect”。它现在的工作方式是用户可以检查gridview上的多个复选框,但我想要一个jquery函数来取消选择gridview上的所有复选框,除了单击的复选框。
I was also looking for a way to access the columns of the checked row with jquery on a button click.
我也在寻找一种方法来通过点击按钮访问jquery的已检查行的列。
Thanks for any help
谢谢你的帮助
Here's how the generated html looks
以下是生成的html的外观
<table class="Grid" cellspacing="0" border="0" id="gvAvailable" style="width:99%;border-collapse:collapse;">
<tr class="GridHeader">
<th scope="col"> </th><th scope="col">Guid</th><th scope="col">Id</th><th scope="col">Name</th>
<th scope="col">Facility</th><th scope="col">Room</th>
</tr>
<tr class="GridItem">
<td>
<input id="gvAvailable_ctl02_chkSelect" type="checkbox" name="gvAvailable$ctl02$chkSelect" />
</td>
<td>24</td>
<td>000101020201</td>
<td>Test</td>
<td>Test Facility</td>
<td> </td>
</tr><tr class="GridAltItem">
<td>
<input id="gvAvailable_ctl03_chkSelect" type="checkbox" name="gvAvailable$ctl03$chkSelect" />
</td>
<td>25</td>
<td>1001</td>
<td>Test 2</td>
<td>Test 3</td>
<td> </td>
</tr>
</table>
4 个解决方案
#1
if you add the same class to each of the checkboxes in the markup, you can retrieve an array of them by saying
如果将相同的类添加到标记中的每个复选框,则可以通过说明检索它们的数组
$('.classname')
This will give you back the array of objects. You can then call 'each' on the array and deselect them all.
这将返回对象数组。然后,您可以在阵列上调用“each”并取消全部取消。
function removeChecks()
{
$('.classname').each(function()
{
$(this).removeAttr('checked');
});
}
Then add the above function call in the click handler for the each checkbox:
然后在每个复选框的单击处理程序中添加以上函数调用:
$('.classname').each(function()
{
$(this).click(function()
{
removeChecks();
$(this).attr('checked', 'checked');
});
});
the code above should be set up to run on page load.
上面的代码应该设置为在页面加载时运行。
#2
In this example I put one button to check, and one button to uncheck gridview checkboxes :
在这个例子中,我放了一个按钮来检查,一个按钮取消选中gridview复选框:
<asp:GridView runat="server" ID="grid"></asp:GridView>
<input type="button" onclick="uncheckCheckBoxes()" value="UnCheck" />
<input type="button" onclick="checkCheckBoxes()" value="Check" />
<script>
function uncheckCheckBoxes()
{
var gridClientID = '<%= grid.ClientID %>';
jQuery.each($("#" + gridClientID + " input[type='checkbox']"), function ()
{
this.checked = false;
});
}
function checkCheckBoxes()
{
var gridClientID = '<%= grid.ClientID %>';
jQuery.each($("#" + gridClientID + " input[type='checkbox']"), function ()
{
this.checked = true;
});
}
</script>
#3
I am assuming you would like to make sure the user clicked checkboxes do not get toggled? If so, go on below.
我假设您要确保用户单击复选框不会切换?如果是这样,请继续下面。
First, just give a class name to all checkboxes in the gridview.
首先,只需为gridview中的所有复选框指定一个类名。
Whenever a checkbox was clicked, add another class to it to denote it was physically selected.
每当单击一个复选框时,向其添加另一个类以表示它是物理选择的。
$('.checkbox').click(function(){
$(this).addClass('checked');
});
Now, when a user clicks on the select all checkbox (let's call it "selectAll") on top, iterate through all the checkboxes and toggle the status while checking the "checked" class
现在,当用户点击顶部的全选复选框(让我们称之为“selectAll”)时,遍历所有复选框并在检查“已检查”类时切换状态
$('#selectAll').click(function(){
var status = $(this).attr('checked')
$('.checkbox').each(function(){
//only toggle if not set
if(!$(this).hasClass('checked')){
if(status){
$(this).attr('checked', 'checked');
}
else{
$(this).attr('checked', '');
}
}
});
});
This should get you along your merry way hopefully.
这应该有希望让你顺利。
Now, accessing columns of the checked row?
现在,访问已检查行的列?
You could add an onclick event to each table row.
$('#tablename tr').click(function(){
//do something
});
#4
i found this articale very usefull Check/Uncheck all Items in an ASP.NET CheckBox List using jQuery
我发现这个非常有用的使用jQuery检查/取消选中ASP.NET CheckBox列表中的所有项目
#1
if you add the same class to each of the checkboxes in the markup, you can retrieve an array of them by saying
如果将相同的类添加到标记中的每个复选框,则可以通过说明检索它们的数组
$('.classname')
This will give you back the array of objects. You can then call 'each' on the array and deselect them all.
这将返回对象数组。然后,您可以在阵列上调用“each”并取消全部取消。
function removeChecks()
{
$('.classname').each(function()
{
$(this).removeAttr('checked');
});
}
Then add the above function call in the click handler for the each checkbox:
然后在每个复选框的单击处理程序中添加以上函数调用:
$('.classname').each(function()
{
$(this).click(function()
{
removeChecks();
$(this).attr('checked', 'checked');
});
});
the code above should be set up to run on page load.
上面的代码应该设置为在页面加载时运行。
#2
In this example I put one button to check, and one button to uncheck gridview checkboxes :
在这个例子中,我放了一个按钮来检查,一个按钮取消选中gridview复选框:
<asp:GridView runat="server" ID="grid"></asp:GridView>
<input type="button" onclick="uncheckCheckBoxes()" value="UnCheck" />
<input type="button" onclick="checkCheckBoxes()" value="Check" />
<script>
function uncheckCheckBoxes()
{
var gridClientID = '<%= grid.ClientID %>';
jQuery.each($("#" + gridClientID + " input[type='checkbox']"), function ()
{
this.checked = false;
});
}
function checkCheckBoxes()
{
var gridClientID = '<%= grid.ClientID %>';
jQuery.each($("#" + gridClientID + " input[type='checkbox']"), function ()
{
this.checked = true;
});
}
</script>
#3
I am assuming you would like to make sure the user clicked checkboxes do not get toggled? If so, go on below.
我假设您要确保用户单击复选框不会切换?如果是这样,请继续下面。
First, just give a class name to all checkboxes in the gridview.
首先,只需为gridview中的所有复选框指定一个类名。
Whenever a checkbox was clicked, add another class to it to denote it was physically selected.
每当单击一个复选框时,向其添加另一个类以表示它是物理选择的。
$('.checkbox').click(function(){
$(this).addClass('checked');
});
Now, when a user clicks on the select all checkbox (let's call it "selectAll") on top, iterate through all the checkboxes and toggle the status while checking the "checked" class
现在,当用户点击顶部的全选复选框(让我们称之为“selectAll”)时,遍历所有复选框并在检查“已检查”类时切换状态
$('#selectAll').click(function(){
var status = $(this).attr('checked')
$('.checkbox').each(function(){
//only toggle if not set
if(!$(this).hasClass('checked')){
if(status){
$(this).attr('checked', 'checked');
}
else{
$(this).attr('checked', '');
}
}
});
});
This should get you along your merry way hopefully.
这应该有希望让你顺利。
Now, accessing columns of the checked row?
现在,访问已检查行的列?
You could add an onclick event to each table row.
$('#tablename tr').click(function(){
//do something
});
#4
i found this articale very usefull Check/Uncheck all Items in an ASP.NET CheckBox List using jQuery
我发现这个非常有用的使用jQuery检查/取消选中ASP.NET CheckBox列表中的所有项目