gpt4 book ai didi

javascript - js脚本中无法识别的表达式

转载 作者:行者123 更新时间:2023-11-30 07:22:47 24 4
gpt4 key购买 nike

我是 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>

控制台报错是这样的

Uncaught Error: Syntax error, unrecognized expression: #voteUp$("#"+image_id).find("#voteUp a")

知道为什么按钮不会变成灰色(禁用)以及为什么会出现此错误吗?

最佳答案

你需要替换这一行:

$("#"+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>

关于javascript - js脚本中无法识别的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32176884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com