错误在哪里:Java脚本代码

时间:2018-01-15 08:47:42

标签: javascript jquery

Aplication建立在Yii2上,使用Kartik的星级小部件。 Js代码有错误:

控制台中出错的图片:

Uncaught SyntaxError: Unexpected token (

代码:

    <?php 
    $js = <<<JS
                            function (event, value, caption) { 
                                $.ajax({
                                    type: 'GET', 
                                    url: '/rating/update-rating', 
                                    data: { 
                                        points: value, 
                                        post_id: $post->id
                                    }, 
                                    success: function(res) { 
                                        $(event.currentTarget).rating('update', res); 
                                    }, 
                                    error: function(e) {  
                                        console.log(e); 
                                    }  
                                }); 
                            }
    JS;
    $this->registerJs($js);

echo StarRating::widget([
                        'name' => $post->post_rate,
                        'value' => isset($post->rating[0]['dec_avg']) ? $post->rating[0]['dec_avg'] : 0,
                        'pluginOptions' => [ ... ] 
                            'pluginEvents' => [
                                'rating:change' => $js,
                            ],

1 个答案:

答案 0 :(得分:2)

您忘记为js函数添加名称:

function functionName(event, value, caption) {
    $.ajax({
        type: 'GET',
        url: '/rating/update-rating',
        data: {
            points: value,
            post_id: $post - > id
        },
        success: function(res) {
            $(event.currentTarget).rating('update', res);
        },
        error: function(e) {
            console.log(e);
        }
    });
}