2014-01-07 32 views
0

我有一个通知/收件箱页面,其中向用户显示收件箱消息列表,我希望他们能够使用JQuery将每条消息标记为正在读取或查看。我有此代码的那一刻:JQuery在多个属性之一上运行函数

<?php echo "<a href=\"#\" id=\"readLink\" data-note=\"$note_id\" class=\"red\">Mark As Read</a>"; ?> 

<script type="text/javascript"> 
$('#readLink').on('click', function() 
{ 
    var note = $(this).attr("data-note"); 
    jQuery.post("php/note-read.php", { 
     note:note 
    }, function(data, textStatus){ 
      if(data == 1){ 
       $('#readLink').html("Note Read"); 
      } 
    }); 
    return false; 
}); 
</script> 

这个脚本适用于显示的最后一条消息,即顶部第一条消息完全没在说20的列表,而不是任何其他人。任何人都可以看到一种方法,使其在列表中的任何消息工作?

+0

将id替换为id,因为id是唯一元素,为什么只检查数据== 1?如果数据每行增加它将工作单链接 –

+0

谢谢@RaunakKathuria - 这允许我在任何属性上运行该功能,但现在它将ALL标记为已读 – user3129227

+0

这是因为您使用'$('。readLink')。 html(“Note Read”);',这意味着用'readLink'类编辑所有链接。我看到我的回答下面 – Curious

回答

0

我认为决定是设置class而不是id。该代码是这样的:

<?php echo "<a href=\"#\" class=\"readLink\" data-note=\"$note_id\" class=\"red\">Mark As Read</a>"; ?> 

<script type="text/javascript"> 
$('.readLink').on('click', function() 
{ 
    var $link = $(this); //remember 'this' in a variable... 
    var note = $(this).attr("data-note"); 
    jQuery.post("php/note-read.php", { 
     note:note 
    }, function(data, textStatus){ 
     if(data == 1){ 
      $link.html("Note Read"); //...and use this variable, because 'this' would not be the link 
     } 
    }); 
    return false; 
}); 
</script> 
+0

谢谢 - 这正是我所需要的,现在工作正常 – user3129227

+0

不客气:) – Curious

0
  • 进入后
  • 前更换ID与类
  • 更换$('.readlink').html("Note Read");that.html("Note Read");所以选择了它只会更新/点击链接,即$(本)

更新JS

<?php echo "<a href=\"#\" class=\"readLink\" data-note=\"$note_id\" class=\"red\">Mark As Read</a>"; ?> 

<script type="text/javascript"> 
$('.readLink').on('click', function() 
{ 

    var that = $(this); 
    var note = that.attr("data-note"); 
    jQuery.post("php/note-read.php", { 
     note:note 
    }, function(data, textStatus){ 
      if(data == 1){ 
       that.html("Note Read"); //replace readlink class with this 
      } 
    }); 
    return false; 
}); 
</script> 
+0

我猜在'$(this).html(“Note Read”)的情况下;''this'将是jQuery AJAX对象,而不是链接 – Curious

相关问题