jQuery投票系统

所以我正在制作一个投票系统,基本上是一个Thumbs Up&Thumbs Down投票系统。 我正在使用CakePHP和jQuery与MySQL,但想确保前端是正确的,这是最好的方法。

我希望用户能够更改他们的投票, 所以利用jQuery这是最好和最有效的方式吗? 在类操作方面,我对jQuery很新手。

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

这是代码。

link('jquery/jquery-1.4.2.min',false); ?>  $(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"); } } }); });  
+ | -

你可以这样做:

  
+ | -

它消除了对额外的html id的需要,你不需要加倍的代码来添加当前的类。 我不确定哪个更快,但我认为这样可以更好地维护代码。

是的,jQuery是最好的解决方案,但我不确定你是否可以优化你的代码。

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