0

出于某种原因,以下脚本抛出错误“错误:语法错误,无法识别的表达式:commentscontainer_(post_id)”..有人知道为什么吗?是语法错误吗?

 $(document).ready(function() {
var msg = '.text-holder';

$('.time').timeago();
$('.text-holder').autosize();

$('#post_comment').click(function() {
    $(msg).focus();
});

$('.text-holder').keypress(function(e) {
    var postID = this.id.replace('post_', '');
    if(e.which == 13) {
        var val = $(msg).val();

        $.ajax({
            url: 'auth/classes/comments.php',
            type: 'GET',
            data: 'token=<?php echo $token; ?>&msg='+escape(val),
            success: function(data) {
                $(msg).val('');
                $(msg).css('height','14px');
                $('.commentscontainer_'+postID).append(data);
                $('.time').timeago();
            }
        });
    }
});

这是相当多的html

<div class="commentscontainer_['post_id']"></div>

我有另一个脚本..它与这个冲突吗?

    $(document).ready(function(){
        $(".comment_box").hide();

        $('.slider').on('click',function(){
            var flag   = $(this).data('flag'),
                postID = this.id.replace('post_', '');

            $('#' + postID).slideToggle();

            if (!flag) {
                $.ajax({
                    url : 'auth/classes/comments.php',
                    data: {post_id: postID}
                }).done(function(data) {
                    $('.commentscontainer_'+postID).replaceWith(data);
                    $('.time').timeago();
                });
            }
            $(this).data('flag', !flag)
        });
    });
4

2 回答 2

0

您还需要删除引号postID

var postID = this.id.replace('post_', '');
postID = postID.replace(/['"]/g,'');
于 2013-09-16T23:05:45.227 回答
0

您必须在选择器中转义某些字符(例如 [, ]),否则 jQuery 会抛出错误。

var postID = this.id.replace('post_', '');
postID = postID.replace(/\[/g,'\\[');
postID = postID.replace(/\]/g,'\\]');
于 2013-09-16T23:29:45.833 回答