Asp.net 用于gridview操作的Jquery函数,带有复选框
my gridview gvAvailable中的第一列是当前复选框列chkSelect。 它现在的工作方式是,用户可以选中gridview上的多个复选框,但我希望jquery函数可以取消选中gridview上的所有复选框,除了单击的复选框 我还在寻找一种方法,通过单击按钮使用jquery访问选中行的列 谢谢你的帮助 下面是生成的html的外观Asp.net 用于gridview操作的Jquery函数,带有复选框,asp.net,jquery,gridview,Asp.net,Jquery,Gridview,my gridview gvAvailable中的第一列是当前复选框列chkSelect。 它现在的工作方式是,用户可以选中gridview上的多个复选框,但我希望jquery函数可以取消选中gridview上的所有复选框,除了单击的复选框 我还在寻找一种方法,通过单击按钮使用jquery访问选中行的列 谢谢你的帮助 下面是生成的html的外观 <table class="Grid" cellspacing="0" border="0" id="gvAvailable" style="wi
<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>
我发现articale非常有用,可以使用jQuery选中/取消选中ASP.NET复选框列表中的所有项目。如果将相同的类添加到标记中的每个复选框中,可以通过以下方式检索它们的数组
$('.classname')
这将返回对象数组。然后可以调用数组中的“each”并取消选择它们
function removeChecks()
{
$('.classname').each(function()
{
$(this).removeAttr('checked');
});
}
然后在每个复选框的单击处理程序中添加上述函数调用:
$('.classname').each(function()
{
$(this).click(function()
{
removeChecks();
$(this).attr('checked', 'checked');
});
});
上面的代码应该设置为在页面加载时运行。我假设您希望确保用户单击的复选框不会被切换?如果是这样,请继续往下看 首先,只需为gridview中的所有复选框指定一个类名 每当单击复选框时,向其添加另一个类以表示它已被物理选中
$('.checkbox').click(function(){
$(this).addClass('checked');
});
现在,当用户单击selectAll复选框时,我们将其称为selectAll on top,遍历所有复选框并在选中checked类的同时切换状态
$('#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', '');
}
}
});
});
希望这能让你过上快乐的生活
现在,访问选中行的列
You could add an onclick event to each table row.
$('#tablename tr').click(function(){
//do something
});
在本例中,我将一个按钮置于选中状态,另一个按钮置于取消选中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>
你能发布一些生成的HTML吗?我知道jQuery,但我不是一个.NET的人,所以我不知道gridview是什么,也不知道它会显示什么。我尝试过使用它,但出于某种原因,什么都没有发生。我将checkbox控件类更改为checked,并在中添加了这两个函数,但它仍然允许我在GridView彩弹中选择多个复选框。您知道我可以使用什么方法来访问按钮单击事件中选中的行和列吗?在jquery中,您可以相当轻松地访问元素的父元素,以及DOM树中元素的同级元素。要访问同一区域中所有元素的数组,可以选择复选框的父项的同级。代码:$'checkbox'。parent.sides将返回和checkbox位于同一行中的元素数组。从那里你可以对这些元素做任何你想做的事情。