简体   繁体   English

jQuery / Javascript:动态添加图片和文本

[英]Jquery/Javascript: adding pictures and text dynamically

Forgive me if this is a simple question, but I am kinda novice to Jquery/Javascript. 如果这是一个简单的问题,请原谅我,但我是Jquery / Javascript的新手。 What I would like to know is how I can add an image and a comment at the same time with one press on the enter key. 我想知道的是如何通过按Enter键一次添加图像和注释。 What I mean is: a user types in a comment in a textarea, hits enter and his/her comment appears in a div together with the username. 我的意思是:用户在文本区域中输入评论,按Enter键,他/她的评论与用户名一起显示在div中。 What I want is that the profile picture corresponding to that user also appears, as well as the option to rate or favourite the comment. 我想要的是也出现了与该用户相对应的个人资料图片,以及对评论进行评分或喜欢的选项。 What I have got so far is following: 到目前为止,我得到的是:

$(document).ready(function() {
    $('.comment').keyup(function(e) {
        if (e.keyCode == 13){
            var post_id = $(this).attr('post_id');
            var comment = $(this).val();
            $(this).val('');
            $.post('../php_includes/comment.php', {post_id: post_id, comment: comment});
            $(this).parent().children('.comments').append("<div class='view'><?php echo $_username;?> remarks: " + comment + "</div>");     
        }
    });
});

This works. 这可行。 In the div "view" appears: username remarks: comment. 在div中,“视图”出现:用户名备注:注释。 However, when I do this: 但是,当我这样做时:

$(document).ready(function() {
    $('.comment').keyup(function(e) {
        if (e.keyCode == 13){
        var post_id = $(this).attr('post_id');
        var comment = $(this).val();
        $(this).val('');
            $.post('../php_includes/comment.php', {post_id: post_id, comment: comment});
        $(this).parent().children('.comments').append("<div class='view'><?php echo $vis_pic;?><?php echo $_username;?> remarks: " + comment + "</div>");       
        }
    });
});

($vis_pic containing the path to the userpic) nothing happens - it does not even clear the textarea. ($ vis_pic包含userpic的路径)没有任何反应-它甚至不会清除文本区域。 I tried to append, prepend, declare the html in a variable - it just does not want to eat it. 我试图在变量中追加,前置,声明html-它只是不想吃掉它。 Also, when I add the php variable to display the rate and favourite options after adding the comment, it does nothing. 另外,在添加注释后添加php变量以显示费率和收藏夹选项时,它什么也没有做。 This is driving me nuts as I scavenged all over Inet for a couple of days now and nothing seems to work. 这让我发疯了,因为我已经在Inet上进行了整整几天的清理工作,但似乎没有任何效果。 Again, sorry if this is a simple question. 再次,很抱歉,如果这是一个简单的问题。 Thank you very much in advance for your efforts. 预先非常感谢您的努力。

Instead of showing php code you should show the result of evaluating it. 与其显示php代码,不如显示评估结果。 I think the problem is that $vis_pic contains double quotes you should escape: 我认为问题在于$vis_pic包含双引号,您应该转义:

<?php echo str_replace('"', '\"', $vis_pic); ?>

Maybe you could also use addslashes 也许你也可以使用和addslashes

Edit: 编辑:

You say $vis_pic contains 您说$vis_pic包含

$vis_pic ="<img class=\"commentpic\" src=\"../user/$_userv/mini_profilepic.jpg\">";

The problem is that you escaped the quotes for php, but when you use echo $vis_pic , php doesn't print \\ , so your code becomes: 问题是您转义了php的引号,但是当您使用echo $vis_pic ,php不会输出\\ ,因此您的代码变为:

.append("<div class='view'><img class="commentpic" src="../user/[userv]/mini_profilepic.jpg"> remarks: " + comment + "</div>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM