【发布时间】:2011-08-21 15:44:52
【问题描述】:
我有一个使用 jQuery 的 Ajax 'like' 按钮,该按钮将在页面上多次使用。我真的不想多次包含 jQuery 脚本,所以有没有办法让 jQuery 为页面上的所有类似按钮工作,但使用每个帖子的唯一 ID。这可能是一个菜鸟问题,但我就是这样。您可以提供的任何解决方案都会很棒。谢谢!
代码:
jQuery:
<script type="text/javascript">
$(function() {
$(".like").click(function(){
$("#loading").html('<img src="loader.gif" align="absmiddle"> loading...');
$.ajax({
type: "POST",
url: "like.php?id=<?php $row['uid']; ?>", // UNIQUE ID, EVERY POST WILL HAVE ONE
success: function(){
$("#loading").ajaxComplete(function(){}).slideUp();
$('#like'+I).fadeOut(200).hide();
$('#remove'+I).fadeIn(200).show();
}
});
return false;
});
});
</script>
<script type="text/javascript">
$(function() {
$(".remove").click(function(){
$("#loading").html('<img src="loader.gif" align="absmiddle"> loading...');
$.ajax({
type: "POST",
url: "remove.php?id=<?php $row['uid']; ?>", /// UNIQUE ID, EVERY POST WILL HAVE ONE
success: function(){
$("#loading").ajaxComplete(function(){}).slideUp();
$('#remove'+I).fadeOut(200).hide();
$('#like'+I).fadeIn(200).show();
}
});
return false;
});
});
</script>
HTML:
<div id="follow1"><a href="#" class="follow" id="1">
<span style="font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif; font-weight: bold; color:#7391AC; text-align:left; font-size:20px; text-align:left;"> <strong>like</strong>
</span></a>
</div>
<div id="remove1" style="display:none"><a href="#" class="remove" id="1">
<span class="remove_b" style="font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif; font-weight: bold; color:#7391AC; text-align:left; font-size:20px; text-align:left;">unlike
</span></a>
</div>
【问题讨论】:
标签: php javascript jquery ajax jquery-ui