我完全是js
的新手,需要一些帮助。这是我的vote.js
脚本
function vote(type, value, image_id) {
var dataFields = {'type': type, 'value': value, 'image_id': image_id};
var ThisImageVotes = $("#"+image_id).find('#'+type).html();
$.ajax({ // Ajax
type: "POST",
url: "add_vots.php",
data: dataFields,
timeout: 3000,
success: function(dataBack){
$("#"+image_id).find('#'+type).html(dataBack);
if(type=='positive')
{
$("#"+image_id).find('#voteUp a').replaceWith('<a href="#" onclick="return false;"></a>');
}
else if(type=='negative')
{
$("#"+image_id).find('#voteDown a').replaceWith('<a href="#" onclick="return false;"></a>');
}
$("#"+image_id).find('#voteUp("#"+image_id).find("#voteUp a")').attr('class', 'vote_up_done oneLine');
$("#"+image_id).find('#voteDown').attr('class', 'vote_down_done oneLine');
$("#"+image_id).find('#positive').attr('class', 'numberVoted oneLine');
$("#"+image_id).find('#negative').attr('class', 'numberVoted oneLine');
$('#message').html('<div id="alertFadeOut" style="color: green">Your vote is added!</div>');
$('#alertFadeOut').fadeOut(3000, function () {
$('#alertFadeOut').text('');
});
},
error: function() {
$('#message').text('Error! Please try again!');
}
});
}
这个脚本的作用是让人们对图像进行投票,以及何时进行投票以使按钮(拇指向上/向下)变灰,即禁用。通常情况下,按钮必须在投票结束后立即变为灰色。目前投票被记录到数据库中,但按钮不会变成灰色,我可以尽可能多地投票。刷新页面后,将禁用按钮。
如果需要,这是带有图片的页面中的HTML部分。
<div id="'.$row['image_id'].'" class="pull-right">
<div id="lineBlock">Ratings: ';
if (isset($_COOKIE[$cookie_name])) {
echo '<div class="vote_up_done oneLine"></div>
<div class="numberVoted oneLine">'.$vote['positive'].'</div>';
} else {
echo '<div class="vote_up oneLine" id="voteUp"><a href="#" onclick="vote(\'positive\', \'1\', '.$row['image_id'].'); return false;"></a></div>
<div class="number oneLine" id="positive">'.$vote['positive'].'</div>';
}
if (isset($_COOKIE[$cookie_name])) {
echo '<div class="vote_down_done oneLine"></div>
<div class="numberVoted oneLine">'.$vote['negative'].'</div>';
} else {
echo '<div class="vote_down oneLine" id="voteDown"><a href="#" onclick="vote(\'negative\', \'1\', '.$row['image_id'].'); return false;"></a></div>
<div class="number oneLine" id="negative">'.$vote['negative'].'</div>';
}
echo '</div><span id="message"></span>
</div>
控制台中的错误是
未捕获错误:语法错误,无法识别的表达式:#voteUp $(“#”+ image_id).find(“#voteUp a”)
任何想法为什么按钮不会变为灰色(禁用)以及为什么会出现此错误?
答案 0 :(得分:4)
您需要替换此行:
$("#"+image_id).find('#voteUp("#"+image_id).find("#voteUp a")').attr('class', 'vote_up_done oneLine');
使用:
$("#"+image_id).find("#voteUp a").attr('class', 'vote_up_done oneLine');
这看起来像是一个严重的拼写错误。你的报价是乱七八糟的。
<强>段强>
$(function () {
$(".actions a").click(function () {
if ($(this).hasClass("disabled"))
return false;
$(this).closest(".actions").find(".disabled").removeClass("disabled");
$(this).addClass("disabled");
return false;
});
});
* {padding: 0; margin: 0; list-style: none; font-family: 'Segoe UI'; text-decoration: none; color: #000;}
li {padding: 5px; border: 1px solid #999; width: 200px; border-radius: 3px; margin: 10px;}
li .actions {overflow: hidden; padding: 3px;}
li .actions a {float: right;}
li .actions a:first-child {float: left;}
li .actions a.disabled, li .actions a.disabled i {color: #999;}
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" />
<ul>
<li>
<div class="image"><img src="http://lorempixel.com/200/100/animals/1" alt="" /></div>
<div class="actions"><a href="#"><i class="fa fa-thumbs-up"></i> Vote Up</a><a href="#"><i class="fa fa-thumbs-down"></i> Vote Down</a></div>
</li>
<li>
<div class="image"><img src="http://lorempixel.com/200/100/animals/2" alt="" /></div>
<div class="actions"><a href="#"><i class="fa fa-thumbs-up"></i> Vote Up</a><a href="#"><i class="fa fa-thumbs-down"></i> Vote Down</a></div>
</li>
<li>
<div class="image"><img src="http://lorempixel.com/200/100/animals/3" alt="" /></div>
<div class="actions"><a href="#"><i class="fa fa-thumbs-up"></i> Vote Up</a><a href="#"><i class="fa fa-thumbs-down"></i> Vote Down</a></div>
</li>
<li>
<div class="image"><img src="http://lorempixel.com/200/100/animals/4" alt="" /></div>
<div class="actions"><a href="#"><i class="fa fa-thumbs-up"></i> Vote Up</a><a href="#"><i class="fa fa-thumbs-down"></i> Vote Down</a></div>
</li>
<li>
<div class="image"><img src="http://lorempixel.com/200/100/animals/5" alt="" /></div>
<div class="actions"><a href="#"><i class="fa fa-thumbs-up"></i> Vote Up</a><a href="#"><i class="fa fa-thumbs-down"></i> Vote Down</a></div>
</li>
</ul>