简体   繁体   English

在div中显示javascript变量的值

[英]Display value of javascript variable in div

Good day all, 大家好

I have the following html code 我有以下html代码

<form method="post" action="blog_comment_post.php" id="post_blog_comment">
    <input type="hidden" value="<?php echo $postid; ?>" name="blogid" id="blogid"/>
<textarea name="blog_comment" class="blog_comment_form" id="blog_comment" placeholder="Join the discussion"></textarea>
</form>

and, I have the following javascript code 而且,我有以下javascript代码

 <script type="text/javascript">
     $(document).ready(function() {
     $("#post_blog_comment").keypress(function(evt) {
            if(evt.which == 13) {
               var commentform = $("#post_blog_comment");
               var blogid = $("#blogid").val();
               var comment = $("#blog_comment").val();
               $.post("blog_comment_post.php", { blogid: blogid, comment: comment},
                   function(data) {

                    var newmedia =
                    '<div class="blog_comm_hold"> \
                    <div class="user_comment_photo1"></div> \
                    <div class="blog_comment_"> \
                        <div class="blog_com_text">\
                        comment\
                        </div>\
                        </div>\
                    <br>\
                        </div>';

                    commentform.after(newmedia);
                    $('#post_blog_comment')[0].reset();
                });
             }
         });
     });
 </script>

What I am trying to do is have the value that I typed into the textarea field of the form be displayed after the form when the user hits the enter key. 我想做的是让我在用户按下Enter键时在表单之后显示在表单的textarea字段中键入的值。 The div classes load well but I don't know how to go about getting the actual value from the var variable be displayed too. div类加载良好,但是我也不知道如何显示var变量中的实际值。

The var comment variable as can be seen above is not displaying its value. 如上所示,var注释变量未显示其值。 The variable is found under the blog_com_text div in the above script. 该变量位于上述脚本的blog_com_text div下。 I want that when the user hits the enter key, that the value of the above comment variable is loaded inside of the respective div based on the above code. 我希望当用户按下Enter键时,基于上面的代码将以上注释变量的值加载到各个div的内部。 The div classes load well with no issue but how to have the value of the variable be loaded too. div类加载良好,没有问题,但是如何也加载变量的值。

Thanks much. 非常感谢。

Here you go with the solution https://jsfiddle.net/xabt3vgt/ 在这里,您可以使用解决方案https://jsfiddle.net/xabt3vgt/

 $(document).ready(function() { $("#post_blog_comment").keypress(function(evt) { if(evt.which == 13) { var commentform = $("#post_blog_comment"); var blogid = $("#blogid").val(); var comment = $("#blog_comment").val(); //$.post("blog_comment_post.php", { blogid: blogid, comment: comment}, // function(data) { var newmedia =`<div class="blog_comm_hold"> <div class="user_comment_photo1"></div> <div class="blog_comment_"> <div class="blog_com_text"> ${comment} </div> </div> <br> </div>`; commentform.after(newmedia); $('#post_blog_comment')[0].reset(); //}); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form method="post" action="blog_comment_post.php" id="post_blog_comment"> <input type="hidden" value="<?php echo $postid; ?>" name="blogid" id="blogid"/> <textarea name="blog_comment" class="blog_comment_form" id="blog_comment" placeholder="Join the discussion"></textarea> </form> 

You just need to uncomment you php ajax call. 您只需要取消注释php ajax调用即可。

Use back tick ` (left to 1) in the keyboard and if you want to add any variable then use ${variable_name} in between the back tick. 在键盘上使用反勾 ` (从左至1),如果要添加任何变量,请在反勾之间使用$ {variable_name}

Try this 尝试这个

<script type="text/javascript">
 $(document).ready(function() {
 $("#post_blog_comment").keypress(function(evt) {
        if(evt.which == 13) {
           var commentform = $("#post_blog_comment");
           var blogid = $("#blogid").val();
           var comment = $("#blog_comment").val();
           $.post("blog_comment_post.php", { blogid: blogid, comment: comment},
               function(data) {

                var newmedia = $('<div class="blog_comm_hold"><div class="user_comment_photo1" /><div class="blog_com_text" /><br></div>');
                newmedia.find('.blog_com_text').html(comment));
                commentform.after(newmedia);
                $('#post_blog_comment')[0].reset();
            });
         }
     });
 });

You can test with this: 您可以对此进行测试:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="post_blog_comment"></div> <textarea id="blog_comment"></textarea> <input type="button" value="Go" id="postcomment" /> <script type="text/javascript"> $('#postcomment').click(function() { showcomment(); }); function showcomment() { var comment = $("#blog_comment").val(); var newmedia = $('<div class="blog_comm_hold"><div class="user_comment_photo1" /><div class="blog_com_text" /><br></div>'); newmedia.find('.blog_com_text').html(comment); $('#post_blog_comment').after(newmedia); $("#blog_comment").val(''); } </script> 

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

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