javascript - 如何减少这个脚本

标签 javascript jquery

我对 Jquery 和 Javascript 很陌生,但我创建了一个与 symfony2 中的 php Controller 通信的脚本。

我的问题是,我能做些什么来减少脚本长度 --> 也许有一种更聪明的方法来解决我用 switch 表达式解决的逻辑?

谢谢

/**
 * This function gets the current clicked buttons value and post it to the given action + returns the correct value
 */

$(document).ready(function() {
    $('button.rating-button').click(function(event) {
        event.preventDefault();
        var $element   = $(this).attr('id');
        var $rating    = $(this).attr('value');
        var $messageId = $(this).closest('ul').next('input:hidden').val();

        if(typeof $rating !== 'undefined' && $rating != null) {
            $.post(Routing.generate('rating_vote', { 'messageId': $messageId, 'ratingCode': $rating }),
            function($json) {
                $('button#'+$element).parent().find('span').text($json.numberOfRatings);

                /* check if oldRating is defined and oldRating is not the same as new rating ( prevent decrease of actual value if this is the first vote entry for the current message and the specific user ) */
                if(typeof $json.oldRating !== 'undefined' && $json.oldRating != null && ($json.oldRating != $rating) ) {
                    switch($json.oldRating) {
                        case 1:
                            $oldElement = 'rating-first';
                            break
                        case 2:
                            $oldElement = 'rating-second';
                            break;
                        case 3:
                            $oldElement = 'rating-third';
                            break;
                    }
                    $('button#'+$oldElement).parent().find('span').text(Number($('button#'+$oldElement).parent().find('span').text()) -1);
                }

            });
        }

    });
});

最佳答案

您可以将 switch case 逻辑替换为 this rather neat concept .

$(function() {
    $('button.rating-button').on('click',function(event) {
        event.preventDefault();
        var $this = $(this),
            element   = $this.attr('id'),
            rating    = $this.attr('value'),
            messageId = $this.closest('ul').next('input:hidden').val();
        if(typeof rating !== 'undefined' && rating != null) {
            $.post(Routing.generate('rating_vote', { 'messageId': messageId, 'ratingCode': rating }),
                function(json) {
                    $('button#'+element).parent().find('span').text(json.numberOfRatings);
                    if(typeof json.oldRating !== 'undefined' && json.oldRating != null && (json.oldRating != rating) ) {
                        var oldElement = ({
                            1: 'rating-first',
                            2: 'rating-second',
                            3: 'rating-third'
                        })[json.oldRating];
                        $('button#'+oldElement).parent().find('span').text(Number($('button#'+oldElement).parent().find('span').text()) -1);
                    }
                }
            );
        }
    });
});

一些总体想法:

  • 使用 jQuery 时,使用 $[varname] 作为包含 jQuery 对象的变量的指示符(了解您的 php 背景,一开始可能会感觉很奇怪)
  • 创建 jQuery 对象对性能来说有点沉重,因此最好确保只创建一次

关于javascript - 如何减少这个脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14584813/

相关文章:

javascript - 如何使用它获得选择器

javascript - 为什么 eval ("475957E-8905") == "475957E-8905"是真的?

javascript - 禁用浏览器页面中的 PDF 加载

javascript - 另一个ajax请求中的jquery ajax

javascript - 当我按下按钮时调用函数

javascript - 动态添加和删除的选择框

javascript - knockout 嵌套组件 : $(document). Ready() ...在加载嵌套组件之前运行

javascript - 将 [1,2] 和 [7,8] 合并为 [[1,7], [2,8]] 的最有效方法是什么

jquery - 如何添加一个类到对应的popup中

jquery - 如何使用 gMap 将具有多个标记的 map 居中?