由于某种原因,以下脚本抛出错误“错误:语法错误,无法识别的表达式: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)
});
});
答案 0 :(得分:0)
您还需要从postID
中删除引号:
var postID = this.id.replace('post_', '');
postID = postID.replace(/['"]/g,'');
答案 1 :(得分:0)
你必须在选择器中转义某些字符(如[,]),否则jQuery会抛出错误。
var postID = this.id.replace('post_', '');
postID = postID.replace(/\[/g,'\\[');
postID = postID.replace(/\]/g,'\\]');