正确处理 jQuery 的 .live()

发布于 2024-10-03 04:49:55 字数 808 浏览 0 评论 0原文

我目前正在尝试进入 jQuery。我遇到了困难(-;

现在我正在尝试通过 .load() 加载页面

scan_music  : function ()
{
 var target = $('#crawl_results');

 target.load('/admin/scanner/crawl_product_ids/de/music #product_ids', function()
 {
  admin.hide_loading();
 });
 return false;
}

,这工作得很好,所有内容都正确显示。我正在加载的页面中有很多复选框,我想要能够用一个特殊的复选框来切换它们(#check_all 检查 .to_be_checked)。

好吧,这个函数工作正常,但它不适用于 ajax 加载的内容,

check_all_boxes : function ()
{
 var check_all  = $('#check_all'),
     to_be_checked = $('.to_be_checked');

 check_all.live('click', function ()
 {
  to_be_checked.attr('checked', check_all.is(':checked'));
 });
}

我知道我必须使用 .live() 。选择加载的内容(使用 check_all.live() 工作正常)。问题是 .to_be_checked 类不受影响,我希望在这种情况下如何使用 live() 函数来选择它们

。你们可以帮助我(:

最诚挚的问候!

I'm currently trying to get into jQuery. And I'm having a hard time (-;

Right now I'm trying to load a page via .load()

scan_music  : function ()
{
 var target = $('#crawl_results');

 target.load('/admin/scanner/crawl_product_ids/de/music #product_ids', function()
 {
  admin.hide_loading();
 });
 return false;
}

That's working pretty fine and everything's getting displayed correctly. The page I'm loading has lots of checkboxes in it and I want to be able to toggle them all with one special checkbox (#check_all checks .to_be_checked).

Well, that function works fine. But it doesn't work for ajax-loaded content.

check_all_boxes : function ()
{
 var check_all  = $('#check_all'),
     to_be_checked = $('.to_be_checked');

 check_all.live('click', function ()
 {
  to_be_checked.attr('checked', check_all.is(':checked'));
 });
}

I know that I have to use .live() to select the loaded content (working fine with check_all.live()). The problem is that the classes .to_be_checked aren't affected and I have no clue how to use the live() function to select them in that case.

I hope you guys can help me out (:

Best regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此刻的回忆 2024-10-10 04:49:55

您需要在 live 处理程序中再次运行 DOM 查询:

check_all.live('click', function ()
{ 
   $('.to_be_checked').attr('checked', check_all.is(':checked'));
});

这是因为在分配 to_be_checked 时,它包含分配时 DOM 查询的结果,这不是期望的行为。

如果您在处理程序内再次运行查询,您将获得所需的结果。

You need to run the DOM query again inside the live handler:

check_all.live('click', function ()
{ 
   $('.to_be_checked').attr('checked', check_all.is(':checked'));
});

This is because at the time to_be_checked is assigned, it contains the result of the DOM query at the time of assignment, which is not what is the desired behavior.

If you run the query again inside the handler, you will get the desired results.

戒ㄋ 2024-10-10 04:49:55

您的问题存在标准关闭问题, to_be_checked 是在 live 函数之外定义的,因此是在 live() 事件最初绑定时设置的,并且稍后不会更新。在 live() 调用中再次使用选择器。

check_all_boxes : function ()
{
 var check_all  = $('#check_all');

 check_all.live('click', function ()
 {
    $('.to_be_checked').attr('checked', check_all.is(':checked'));
 });
}

Your problem there is a standard closure issue, to_be_checked is defined outside of the live function and is thus set at the time the live() event is initially bound and does not update later. Use the selector again inside the live() call.

check_all_boxes : function ()
{
 var check_all  = $('#check_all');

 check_all.live('click', function ()
 {
    $('.to_be_checked').attr('checked', check_all.is(':checked'));
 });
}
眼眸里的那抹悲凉 2024-10-10 04:49:55
check_all_boxes : function ()
{
 var check_all  = $('#check_all'),
     to_be_checked = $('.to_be_checked');

 check_all.live('click', function ()
 {
  to_be_checked.attr('checked', check_all.is(':checked'));
 });
}

在此代码中,您正在缓存“.to_be_checked”的选择器。这对于选择器来说通常是个好主意,但在通过 AJAX 加载内容时则不然。这里发生的事情是,当定义“to_be_checked”时,它会在 DOM 中搜索具有该类的所有元素,创建对象并创建对象,仅此而已。

你想要做的就是这样:

check_all.live('click', function () {
      $('.to_be_checked').attr('checked', check_all.is(':checked'));
     });
    }
check_all_boxes : function ()
{
 var check_all  = $('#check_all'),
     to_be_checked = $('.to_be_checked');

 check_all.live('click', function ()
 {
  to_be_checked.attr('checked', check_all.is(':checked'));
 });
}

In this code, you are caching your selector of ".to_be_checked". This is normally a good idea for selectors, but not when loading stuff in through AJAX. What's going on here is that when "to_be_checked" is defined, it searches the DOM for all elements with that class, creates and object, and that's it.

What you'll want to do is just this:

check_all.live('click', function () {
      $('.to_be_checked').attr('checked', check_all.is(':checked'));
     });
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文