简体   繁体   English

Textarea-缺少新行

[英]Textarea - missing new line

I have following code: 我有以下代码:

<form action="../p/padd.php" method="POST"> 
<input type="button" value="Náhľad"  OnClick="javascript:nahlad()" /> 
<textarea tabindex="4" id="textra" name="text" ></textarea>
<input type="submit" value="Vložiť" /> 
</form>

<span id="nahlad"> </span>

<script>
   function nahlad()
   {
     var textra = document.getElementById("textra").value;
     alert (textra);
     var xmlhttp;
     if (window.XMLHttpRequest) {xmlhttp=new XMLHttpRequest();
   }
   else
     {xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
   xmlhttp.onreadystatechange=function()
   {
   if (xmlhttp.readyState==4 && xmlhttp.status==200) 
   {
     document.getElementById('nahlad').innerHTML = xmlhttp.responseText;
   }
  }
xmlhttp.open("GET","ssnahlad.php?text=" + textra,true);
xmlhttp.send();
}
</script>

When I enter the following into the textarea (yes there is "new line" with enter) 当我在textarea中输入以下内容时(是的,在enter中有“换行”)

asd
asd

and click the Nahlad button 然后单击Nahlad按钮

ssnahlad.php contains ssnahlad.php包含

<?php
$new = $_GET['text'];
echo nl2br($new);
?>

so why does the span with id=nahlad contain 那么为什么id = nahlad的跨度包含

asdasd 

instead of 代替

asd
asd  

Now that I'm at home and can test, this will work: 现在我可以在家进行测试了,这可以正常工作:

  var newText = encodeURIComponent(textra);

using encodeURIComponent before you send it will correctly render your output, with no decoding on the server side 在发送前使用encodeURIComponent可以正确呈现您的输出,而在服务器端不进行解码

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

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