所以我正在制作一个投票系统,基本上是一个竖起大拇指的投票系统。我在 MySQL 中使用 CakePHP 和 jQuery,但想确保前端是正确的,这是最好的方法。
我希望用户能够改变他们的投票,所以使用 jQuery 是最好和最有效的方法吗? 在类操作方面,我对 jQuery 相当陌生。
ID 字段将是用户将投票的照片的唯一 ID。当然,这只是一个测试,不会成为生产中的最终产品。页面上会有多张照片,用户对每张照片投赞成票或反对票。
这是代码。
<?php
echo $javascript->link('jquery/jquery-1.4.2.min',false);
?>
<script type="text/javascript">
$(document).ready(function() {
$('.vote').click(function () {
if ($(this).hasClass("current")) {
alert("You have already voted for this option!");
return;
}
var parentId = $(this).parent("div").attr("id");
if ($(this).hasClass("up")) {
//Do backend query and checking here
alert("Voted Up!");
$(this).toggleClass("current");
if ($("#" + parentId + "-down").hasClass("current")) {
$("#" + parentId + "-down").toggleClass("current");
}
}
else if($(this).hasClass("down")) {
//Do backend query and checking here
alert("Voted Down!");
$(this).toggleClass("current");
if ($("#" + parentId + "-up").hasClass("current")) {
$("#" + parentId + "-up").toggleClass("current");
}
}
});
});
</script>
<div id="1234">
<a id="1234-up" class="vote up" >+</a> | <a id="1234-down" class="vote down" >-</a>
</div>