【发布时间】:2011-12-14 13:15:09
【问题描述】:
我有两个“按钮”(喜欢/不喜欢),它们实际上是附加到 href 标记的图像,当用户单击时,值会发送到我的数据库。 #Messages 显示 check.php 的输出,它只返回值。 jQuery 代码与按钮的 html 一起位于循环内。
奇怪的是,我的代码仅适用于“喜欢”按钮,并且只有在“不喜欢”按钮从我的代码中删除时才有效。不喜欢按钮本身(如从代码中删除的按钮)根本不起作用,如果两个按钮都被编码,则两个按钮都不起作用。
此外,#like 应该显示在 #dislike 之前,但在我的页面上,#dislike 出现在 #like 的左侧,这不是应该发生的(它应该出现在 #like 的右侧) .有什么想法吗?
开始循环:
<?php
$data = mysql_query("SELECT * FROM Contests");
while($row = mysql_fetch_array( $data )){
$query = mysql_query("SELECT * FROM userContests WHERE userID='$userID' AND contestID='$row[contestID]';") or die(mysql_error());
$checked = mysql_fetch_assoc($query);
?>
我的jQuery(如果我删除#dislike,那么#like 有效。如果我删除#like,#dislike 不起作用。当两者都存在时,两者都不起作用):
<script type="text/javascript">
$(document).ready(function() {
var checked = <?php echo $checked['value']; ?>;
var postID = <?php echo $row['postID']; ?>;
var userID = <?php echo $current_user->ID; ?>;
if (checked == 1) {
$('#post_<?php echo $row['postID']; ?>').addClass('like');
} else if (checked == 0) {
$('#post_<?php echo $row['postID']; ?>').addClass('dislike');
}
$('#like_<?php echo $row['postID']; ?>').click(function(liked) {
$.ajax({
url: 'check.php',
type: 'POST',
data: 'userID=' + userID + '&postID=' + postID + '&value=' + '1',
success: function(result) {
$('#Message_<?php echo $row['postID']; ?>').html('').html(result).prependTo('#post_<?php echo $row['postID']; ?>');
}
});
$('#post_<?php echo $row['postID']; ?>').removeClass('dislike').addClass('like');
if (showLikes.attr('checked')) {
$('#post_<?php echo $row['postID']; ?>').toggle();
}
});
$('#dislike_<?php echo $row['postID']; ?>').click(function(disliked) {
$.ajax({
url: 'check.php',
type: 'POST',
data: 'userID=' + userID + '&postID=' + postID + '&value=' + '0',
success: function(result) {
$('#Message_<?php echo $row['postID']; ?>').html('').html(result).prependTo('#post_<?php echo $row['postID']; ?>');
}
$('#post_<?php echo $row['postID']; ?>').removeClass('like').addClass('dislike');
if (showDislikes.attr('checked')) {
$('#post_<?php echo $row['postID']; ?>').toggle();
}
});
我的 HTML:
<div id="post_<?php echo $row['postID']; ?>" class="post">
<div id="post_<?php echo $row['postID']; ?>_inside">
<div>
<a id="like_<?php echo $row['postID']; ?>" class="like" href="#"><span></span></a>
</div>
<div>
<a id="dislike_<?php echo $row['postID']; ?>" class="dislike" href="#"><span></span></a>
</div>
<b><?php echo $row['Title']; ?></b><br>
Expires: <?php echo $row['Exp']; ?><br>
<ul id="listM"></ul>
</div>
</div>
<div id="Message_<?php echo $row['postID']; ?>" class="reminder"></div>
结束循环:
<?php
}
?>
我的 CSS:
a.like {
float: right;
background: url(images/entered.png) no-repeat top center;
height: 30px;
width: 28px;
}
a.like:active {
float: right;
background: url(images/entered.png) no-repeat bottom center;
height: 30px;
width: 28px;
}
a.dislike {
float: right;
background: url(images/not-interested.png) no-repeat top center;
height: 30px;
width: 28px;
}
a.dislike:active {
float: right;
background: url(images/not-interested.png) no-repeat bottom center;
height: 30px;
width: 28px;
}
那么可能发生了什么?如果#like 没有#dislike 就可以工作,那么为什么#dislike 没有#like 就不能工作?
【问题讨论】:
-
(仍在寻找,但这段代码将是练习重构的好机会;)是否只有一行,还是在循环中?
-
另外,您可以发布 html 输出而不是 PHP 吗?这将使调查变得更加容易。
-
html 在 php 循环中。所以无论你在哪里看到 php,你都在看一个代表 mysql 中帖子数量的数值。即#Message_1、#post_1、#post_1_inside、#like_1、#dislike_1 等等。我已经更新了 html 代码来展示我的循环。
-
仅供参考给我的问题 -1 的人,它是如何不知情或不清楚的?!
-
与您的问题并没有真正的关系,但是为什么您使用两个单独的查询而不是连接?
标签: jquery mysql ajax button href