PHP将换行符打印到textarea中

时间:2011-04-17 03:40:16

标签: php javascript html textarea newline

我正在尝试将换行符打印到textarea。

我有这个字符串:

$coords = $row->lat.','.$row->lon."\r\n"

当我使用以下javascript命令时:

alert(coords); 

我明白了:

-35.308401,149.124298
-35.307841,149.124298

然而,当我在textarea中查看它时,我得到:

-35.308401,149.124298 -35.307841,149.124298

如何在textarea中显示换行符?

更多信息:

警告窗口显示:

alert window

textarea的: enter image description here

代码:

<textarea name="addrs" rows=5 cols=80>-35.308401,149.124298 -35.307841,149.124298</textarea>

更多信息: 这是创建表单和将文本写入textarea的方式

function openMapWindow (data) {
    alert(data);

    var mapForm = document.createElement("form");
    mapForm.target = "Map";
    mapForm.method = "POST"; // or "post" if appropriate
    mapForm.action = "http://www.xxx.com/map.php";

    var mapInput = document.createElement("input");
    mapInput.type = "text";
    mapInput.name = "addrs";
    mapInput.value = data;
    mapForm.appendChild(mapInput);

    document.body.appendChild(mapForm);

    map = window.open("", "Map", "status=0,title=0,height=600,width=800,scrollbars=1");

    if (map) {
        mapForm.submit();
    } else {
        alert('You must allow popups for this map to work.');
    }

}

function mapSuppliers(customer_id) {
    $.get("get.map.points.php", { c_id: customer_id },
     function(data){
         if (data!='') {
            openMapWindow(data);
         } else {
             alert("Missing map coordinates - cannot display map (data: " + data + ")");
         }
     });

}

2 个答案:

答案 0 :(得分:3)

您正尝试在输入字段中包含多行。

考虑将您的代码更新为:

var mapTextarea = document.createElement("textarea");
mapTextarea.name = "addrs";
mapTextarea.value = data;
mapForm.appendChild(mapTextarea);

答案 1 :(得分:1)

我知道这个问题已经很老了,但我会发布我的答案,万一有人来这里。

例如,如果我们想在textarea中显示此文本:

hi Masume
have a nice day!

使用PHP:

echo "hi Masume".chr(13)."have a nice day!";

使用JS:

textareaElement = 'hi Masume' + String.fromCharCode(13) + 'have a nice day!';