如何使这个jQuery代码与GridView控件一起使用

编辑:什么不工作: – 我有两列(批准所有/拒绝所有)我如何限制用户只允许每个的一个复选框? 如果你不使用gridview,下面的代码工作….

我在这里问过这个问题( 只允许选中一个(批准/拒绝)复选框 )并且只有在我没有使用gridview控件的情况下才能使用asp.net控件意义时才按预期工作,现在我处于我的情况使用gridview控件,似乎我的代码不起作用…我保持了相同的类名。 任何帮助?

这是我的.aspx代码与gridview:

 $(document).ready(function () { $('#C1All').click(function () { debugger $('.col1 > input').attr("checked", $('#C1All').attr("checked")); $('.col2 > input').removeAttr("checked"); $('#C2All').removeAttr("checked"); }); $('#C2All').click(function () { debugger $('.col2 > input').attr("checked", $('#C2All').attr("checked")); $('.col1 > input').removeAttr("checked"); $('#C1All').removeAttr("checked"); }); $('.col1').each(function () { $(this).click(function () { debugger var id = $("input", this).attr('id'); var coresId = id.replace('C1', 'C2'); $('#' + coresId).removeAttr("checked"); $('#C1All').removeAttr("checked"); $('#C2All').removeAttr("checked"); }); }); $('.col2').each(function () { $(this).click(function () {debugger var id = $("input", this).attr('id'); var coresId = id.replace('C2', 'C1'); $('#' + coresId).removeAttr("checked"); $('#C1All').removeAttr("checked"); $('#C2All').removeAttr("checked"); }); }); });    
Approve
Reject
Please select Month At End At Travel
Please select Month At End At Travel

这与控件如何呈现给浏览器有关。 因此,基于此,您将需要修改jQuery元素选择器。 对于你的GridView ,发生的事情是,而不是C1AllC2All它现在是gv_C1Allgv_C2All 。 对于复选框,不是用C2替换C1而是反之亦然,这次你需要用Reject替换Approve这个词,反之亦然。 这是修改过的jQuery。 现在它应该工作。

我建议你查看浏览器中的页面HTML,以便了解控件的呈现方式(可以在InternetExplorer中按function键F12查看开发人员工具窗格,在那里可以看到HTML树)

  $(document).ready(function () { $('#gv_C1All').click(function () { $('.col1 > input').attr("checked", $('#gv_C1All').attr("checked")); $('.col2 > input').removeAttr("checked"); $('#gv_C2All').removeAttr("checked"); }); $('#gv_C2All').click(function () { $('.col2 > input').attr("checked", $('#gv_C2All').attr("checked")); $('.col1 > input').removeAttr("checked"); $('#gv_C1All').removeAttr("checked"); }); $('.col1').each(function () { $(this).click(function () { var id = $("input", this).attr('id'); var coresId = id.replace('Approve', 'Reject'); $('#' + coresId).removeAttr("checked"); $('#gv_C1All').removeAttr("checked"); $('#gv_C2All').removeAttr("checked"); }); }); $('.col2').each(function () { $(this).click(function () { var id = $("input", this).attr('id'); var coresId = id.replace('Reject', 'Approve'); $('#' + coresId).removeAttr("checked"); $('#gv_C1All').removeAttr("checked"); $('#gv_C2All').removeAttr("checked"); }); }); });