7

I have a textarea for users to input comments and a button to submit using jQuery’s .post() and JSON:

$('#submit').click(function () {
    var comment = {};
    comment.Author = $("#author").val();
    comment.Email = $("#email").val();
    comment.WebSite = $("#url").val();
    comment.Body = $("#comment").val(); // textarea

    $.post('/ajax/comments', comment, parseComment, 'json');

But $("#comment").val() does not seem to preserve newlines (the input is losing newlines). How do I get this to work?

4

5 に答える 5

6

テキストエリアは<br>改行のユーザー入力にタグを挿入せず、単に改行文字を含めます\n

このスニペットは、破線のボタンをクリックすると警告ボックスを表示します。

<script type="text/javascript">
  $(document).ready(function(){
    $('#submit').click(function() { alert($('#comment').val()); })
  });
</script>

<textarea id='comment'></textarea>
<input type='submit' id='submit'/>

これらを html ページに表示する必要がある場合は、\n<br>自分で置き換える必要があります。

于 2010-07-03T23:33:59.350 に答える
1

テキストエリアの値でencodeURIComponentを使用し、それをサーバーに送信するだけです..印刷するとき..たとえばPHPの場合はnl2brを使用できます

于 2015-03-10T12:41:31.353 に答える
1

新しい行が失われていますか?この投稿を参照してください。

于 2010-07-03T23:34:02.550 に答える
0

私が見つけた唯一の解決策は、テキストエリアデータをjsonの外部で個別に渡すことです。json 内では \n のようにエスケープする必要がありますが、db からリロードすると新しい行が失われます。

したがって、json の目的で、解析エラーを回避するためにエスケープします。また、この textarea フィールドを別の (json で解析されていない) 変数によってそのまま保存します。

于 2021-02-05T22:00:09.030 に答える