[英]jquery val() will not display value
I am trying to set a value after retrieving comments from a $.ajax call. 我试图从$ .ajax调用中获取评论后设置一个值。 When I alert out the values of commentOutput and commentSpan, they are showing the correct data.
当我提醒commentOutput和commentSpan的值时,它们显示正确的数据。 However, when I try to set the value from those comments in a span, they do not show up, not do they appear in the generated source.
但是,当我尝试从一个范围中的那些注释中设置值时,它们不会显示,也不会出现在生成的源中。
Here is the javascript. 这是JavaScript。 You can see the return from the POST in your console.
您可以在控制台中查看POST的返回信息。 You can also see the value it is grabbing for the span, as I have alerted it onclick.
您还可以看到它正在捕获范围的值,因为我已经在单击时提醒了它。
To invoke this, just click the first picture of the cat, none of the others have comments at this point. 要调用此功能,只需单击猫的第一张图片,此时其他人都没有评论。
var ajaxOptions = {
type: 'POST',
dataType: 'json',
url: 'ajax/comments.php',
data: { id: photoID, action: 'GetComments' },
success: function(data) {
for(i=0; i<data.length; i++) {
var commentOutput = '<div><h4>'+data[i].comment_user+' says:</h4>';
commentOutput += '<span>'+data[i].comment_msg+'</span>';
commentOutput += '</div>';
var commentSpan = '#photo' + data[i].photo_id + ' .comments';
$(commentSpan).val(commentOutput);
$(commentSpan).delay('200').css('display','block');
}
}
};
Any help would be greatly appreciated, 任何帮助将不胜感激,
Cheers, 干杯,
Cody 科迪
Method val()
is used for input
, select
and textarea
elements. val()
方法用于input
, select
和textarea
元素。
You should use html()
or text()
methods instead. 您应该改用
html()
或text()
方法。
NB: That's the exact copy of my recent answer for almost the same question. 注意:这是我最近对几乎同一问题的答案的准确副本。 Have you tried to find the solution using some searching?
您是否尝试过通过搜索找到解决方案?
You want .html()
, not .val()
. 您需要
.html()
,而不是.val()
。
$(commentSpan).html(commentOutput);
Also if your server isn't HTML-escaping the comment data, you should do it in your script: 另外,如果您的服务器未使用HTML将注释数据转义,则应在脚本中执行此操作:
function safe(s) {
return s.replace(/&/g, '&').replace(/>/g, '>').replace(/</g, '<');
}
// ...
var commentOutput = '<div><h4>'+ safe(data[i].comment_user) + ' says:</h4>';
commentOutput += '<span>' + safe(data[i].comment_msg) + '</span>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.