GridView 上的 jQuery 函数在页面回发后停止工作(鼠标悬停、单击)
我的所有 jQuery 函数在第一次加载页面时都能完美运行,在我执行回发后,会为 GridView 填充行过滤器,并根据行过滤器再次绑定网格。我的 GridView 完全由代码构建并呈现为表格。在此回发之后,鼠标悬停、鼠标移出和单击表中的行的功能不再起作用。
我将 jQuery 函数放在 document.ready 函数中。我还注意到在回发之前这被添加到行的 html 中:
<tr jQuery1320916="3">
所以基本上是每行末尾的序列 (3) 和一些随机数,前面有 jQuery。回发后,这将从行中删除。
这是我的 jQuery 代码的一部分(第一次单击功能在 PostBack 之后仍然有效,但 GridView 上的功能不再有效):
$(document).ready(function(){
//start click function select all
$('input[id$="SelectDeselectAllBox"]').click(function(){
//start if
if($(this).is(':checked')){
$(this).parent().parent().parent().parent().children().find('input:checkbox').attr('checked',true).closest('tr').addClass('SelectedRow')
}else{
$(this).parent().parent().parent().parent().children().find('input:checkbox').attr('checked',false).closest('tr').removeClass('SelectedRow')};
//end if
//end click function select all
});
//highligth hovered row
$('table[id^=taskgrid] tbody tr').mouseover(function () {
$(this).addClass('HightlightRow');
}).mouseout(function () {
$(this).removeClass('HightlightRow');
});
//end document ready
});
我是否遗漏了一些东西,提前致谢。
我确实发现jQuery不再通过这种方法找到我的gridview:
$('table[id^=taskgrid] tbody tr'),如果我填写taskgrid的总ID那么它就可以工作。但这对我来说不是选择。
//is everthing checked? start if
if($(this).parent().find('input:checkbox:not(:checked)').length == 0){
$(this).parent().parent().parent().parent().find('input:checkbox:first').attr('checked',true)
}else{
$(this).parent().parent().parent().parent().find('input:checkbox:first').attr('checked',false)};
//end if
All my jQuery functions work perfectly the first time the page is loaded, after I do a postback which causes a rowfilter to be filled in for a GridView and binds the Grid again based on the rowfilter. My GridView is built up totally from code and rendered as a table. After this PostBack the function mouseover, mouseout and click on the table rows in my table do not work anymore.
I place my jQuery functions inside document.ready function. I also noticed before the postback this is added to the html of the rows:
<tr jQuery1320916="3">
So basically a sequence at the end of each row (3) and some random numbers with jQuery in front of it. After the postback this is removed from the rows.
Here is a part of my jQuery code (the first click function still works after PostBack but the functions on the GridView not anymore):
$(document).ready(function(){
//start click function select all
$('input[id$="SelectDeselectAllBox"]').click(function(){
//start if
if($(this).is(':checked')){
$(this).parent().parent().parent().parent().children().find('input:checkbox').attr('checked',true).closest('tr').addClass('SelectedRow')
}else{
$(this).parent().parent().parent().parent().children().find('input:checkbox').attr('checked',false).closest('tr').removeClass('SelectedRow')};
//end if
//end click function select all
});
//highligth hovered row
$('table[id^=taskgrid] tbody tr').mouseover(function () {
$(this).addClass('HightlightRow');
}).mouseout(function () {
$(this).removeClass('HightlightRow');
});
//end document ready
});
Am I missing something, thanks in advance.
I did discover that jQuery does not find my gridview anymore by this method:
$('table[id^=taskgrid] tbody tr'), if I fill in the total ID of the taskgrid then it does work. But this is not option for me.
//is everthing checked? start if
if($(this).parent().find('input:checkbox:not(:checked)').length == 0){
$(this).parent().parent().parent().parent().find('input:checkbox:first').attr('checked',true)
}else{
$(this).parent().parent().parent().parent().find('input:checkbox:first').attr('checked',false)};
//end if
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,您正在使用“开头为”选择器:
对于 ASP.NET WebForms 生成的 id,最好使用“结尾为”选择器。假设您的 GridView id 是“taskgrid”,请尝试使用以下选择器:
如果您的目标是多个表元素并且“taskgrid”只是 id 的一部分,则可以使用“contains”选择器:
The problem is, that you are using "starts with" selector:
It is better to use "ends with" selector for ASP.NET WebForms generated id. Assuming your GridView id is "taskgrid", try using following selector:
In case you are targeting several table elements and "taskgrid" is only part of id, you can use "contains" selector: