2015-09-18 16 views
-3

当我按回车键为帖子开始一个新行时,结果会自动忽略它。例如,我按下'a'键'返回'键'b',它打印出'ab'。但我需要'a
b'我想知道如何解决它?textarea,使用返回键换行不起作用

我试过$comment = nl2br($comment),这是行不通的。

下面是我刚刚进行测试的一个演示。

php文件(linebreak.php)

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
<div id="comment_box" contenteditable="true" autocomplete="off" spellcheck="false" placeholder="How do you feel about?"></div> 
<div id="post_comment" class="comment_leg">Post</div> 
<div name="commentsubmit"></div> 
<style> 
#comment_box{ 
    text-align: center; 
    background-color: white; 

    /*position:relative;*/ 
    border: 1px solid orange; 
    /*height:60px;*/ 
    width: 500px; 
    padding: 10px; 
    color:black; 
    border-radius:3px; 
    /*font-size:18px;*/ 
    display: inline-block; 
    text-align: left; 
    vertical-align: bottom; 

    /*border-color:yellow;*/ 
} 
.comment_leg{ 
cursor:pointer; 
width:60px; 
/*height:18px;*/ 
background-color: #ffcc99; 
padding:5px; 
text-align: center; 
border-radius: 3px; 
display:hide; 
vertical-align: bottom; 
    display: inline-block; 
} 
</style> 


<script> 
$(function(){ 
     $("#post_comment").click(function(){ 
      var txt = $("#comment_box").text(); 
      if(txt){ 
        $.post("commenttest.php", {txt: txt}, function(result){ 
         $("div[name=commentsubmit]").prepend(result); 
         $("#comment_box").text(''); 
        }); 
      } 

     }) 
}) 

</script> 

php文件(commenttest.php)

<?php 
$comment=$_POST["txt"]; 
echo "<div style='color:orange'>".$comment."</div>" 
?> 
+0

为什么有申报单,而不是textarea? – MozzieMD

+3

请不要多次发布[同一个问题](http://stackoverflow.com/q/32658369/1169519)。 – Teemu

+1

@Teemu哈哈,很好! –

回答