开发者

Multiple jQuery on the same page

开发者 https://www.devze.com 2023-02-28 16:30 出处:网络
I have just started to learn jQuery and I think I am getting the hang to it, except this issue. I have a page of comments the user would like to delete. I am used to doing something like this:

I have just started to learn jQuery and I think I am getting the hang to it, except this issue. I have a page of comments the user would like to delete. I am used to doing something like this:

$(function() {

$(".commentdeletebutton").click(function() {

$.ajax({
  type: "POST",
  url: "http://myflashpics.com/process_deletecomment.php",
  data: $("#commentdeleteform").serialize(),
  success: function() {

    // Comment IDS are like this 'comment_123'
    var commentId  = $(comment_id).val();
    var commentDone = "comment_" + commentId;
    $(commentDone).fadeOut();


  }
 });
return false;
});
});

But since there are multiple instances of IDs, it's not working and nothing is happening.

Again, 开发者_C百科newbie here wondering what would be a better way to do this.

Thanks

Coulton


@php ,

First thing you should not have multiple ids on the DOM , this can cause several problems while parsing.

If you want to group them , give a generic class name like

classname + "ID"

later you can extrack the ID from the class and do your logic

you can do something like this

var className=$(this).attr('class');            
        var ID = className.replace(/yourclassname(\d+)/, "$1");


Instead of id attribute use class class attribute.
ID's are unique.


IDs should always be unique. Consider adding something more to your IDs to make them unique.


Somehow related to what @kobe says, I think your best approach is to generate those IDs based on a prefix (like myComment-) and the id in the DB. This way you would end with elements identified by myComment-1, myComment-2, ..., myComment-n .

Then, when you should use a selector for each element that starts with "myComment" (look at the selectors documentation on the jQuery docs) and set it the "click" handler. Accessing $(this) should give you the clicked element and you can get the ID, remove the 'comment-' section programmatically, and once again, retrieve or perform the action or get the required attributes for the original ID.

Hope this helps.


Anyway, it's not clear where comment_id comes from.

If $(".commentdeletebutton") is inside some container, or next to the element you need to fadeOut, you could use some .parent() or .next() selector, without need any ID.

PS: it seems that you are not telling http://myflashpics.com/process_deletecomment.php which comment it must delete either.

0

精彩评论

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