开发者

Changing an attribute on certain future elements when they appear in jQuery - it's possible?

开发者 https://www.devze.com 2023-01-02 08:01 出处:网络
I need a开发者_开发知识库 sort of \"live\" effect on some checkbox - I need that every checkbox since now will be checked by a jQuery function attr(\"checked\", true) when they appear.

I need a开发者_开发知识库 sort of "live" effect on some checkbox - I need that every checkbox since now will be checked by a jQuery function attr("checked", true) when they appear.

Is it possible to do?


You can do this in your $.ajax() success handler, or the success handler of whatever short form of $.ajax() you're using, (e.g. $.post(), $.get(), etc.), like this:

$.ajax({
  //options...
  success: function(data) {
    $("input:checkbox", data).attr("checked", true);
  }
});

This is the most optimal way to accomplish what you're after, by giving it a context to look in...so it's only looking for input:checkbox in the returned result you're now adding to the DOM, not the entire page.

If that's not possible, you can use something like the livequery plugin, like this:

$("input:checkbox").livequery(function() {
  $(this).attr("checked", true);
});

This is more expensive due to the way it looks for new elements to be added, but gets the job done.

0

精彩评论

暂无评论...
验证码 换一张
取 消