首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery投票系统

jQuery投票系统
EN

Stack Overflow用户
提问于 2010-09-16 00:28:31
回答 3查看 3K关注 0票数 4

所以我要做一个投票系统,基本上是一个大拇指和大拇指的投票系统。我将CakePHP和jQuery与MySQL结合使用,但希望确保前端是正确的,这是最好的方法。

我希望用户能够改变他们的投票方式,,所以使用jQuery是最好和最有效的方法?--当涉及到类操作时,是jQuery的新手。

ID字段将是用户投票的照片的唯一id。当然,这只是一个测试,而这并不是生产中的最终产品。页面上会有多张照片,用户会为每一张照片投上或下票。

这是密码。

代码语言:javascript
复制
<?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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-09-16 04:26:12

你可以这样做:

代码语言:javascript
复制
<script type="text/javascript">
    $(document).ready(function() {
        $('.vote').click(function () {
            if ($(this).hasClass("current")) {
                alert("You have already voted for this option!");
            } else {
                var parentId = $(this).parent("div").attr("id");
                var error = false;

                if ($(this).hasClass("up")) {
                    //Do backend query and checking here (SET: error)
                    alert("Voted Up!");
                }
                else if($(this).hasClass("down")) {
                    //Do backend query and checking here (SET: error)
                    alert("Voted Down!");
                }
                //removes all the votes 
                $(this).parent("div").children().removeClass("current");

                if(!error) {
                    $(this).addClass("current");
                } else {
                    alert("There was an error");
                }
            }
            return false;
        });
    });
</script>
<div id="1234">
    <a class="vote up" href="#">+</a> | <a class="vote down" href="#">-</a>
</div>

它消除了额外的html id的需要,并且您也不需要加倍的代码来添加当前类。我不确定哪个更快,但我认为这种方式将允许更好的代码维护。

票数 5
EN

Stack Overflow用户

发布于 2010-09-16 04:17:32

是的,jQuery是这方面的最佳解决方案,但我不确定您是否还能对代码进行优化。

票数 0
EN

Stack Overflow用户

发布于 2011-02-17 02:37:11

您可以使用这个:http://www.technabled.com/2011/02/pulse-lite-reddit-ajax-up-down-voting.html信息:我是开发人员。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3722874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档