ASP。NET MVC + jQuery——当我$的时候,我如何保存。NET的结尾。通过jQuery发布表单?

时间:2022-02-08 08:11:12

Here is what my jQuery code is doing:

下面是我的jQuery代码:

 $('.newCommentBox form').live('submit', function () {

    //Submit the form post
    $.post($(this).attr('action'), $(this).serialize(), function (data) { showNewComment(data) });

    return false;
});

Whenever a form is submitted, I take the results, serialize them, and send them to the specific action via a POST. When we get a response back from the server, I invoke the showNewComment function to display the comment being added in real-time.

每当提交表单时,我就会对结果进行序列化,并通过POST将其发送到特定的操作。当我们从服务器得到响应时,我调用showNewComment函数来实时显示添加的注释。

Here is my issue: I need to be able to preserve the whitespace people leave in their comments so they can control the readability of longer comments. I use a .NET Html helper method to convert CRLF linebreaks into <br/> tags when the comments get rendered, but all of those line-endings are not preserved when the form is submitted via jQuery and serialized via JSON, whereas when I did this without AJAX it worked just fine.

这是我的问题:我需要能够保留人们在评论中留下的空白,这样他们就可以控制更长的注释的可读性。我使用。net Html helper方法在注释被呈现时将CRLF换行符转换为
标记,但是在通过jQuery提交表单和通过JSON序列化表单时,所有这些行结尾都没有保留下来,而在没有使用AJAX的情况下,我这样做是很有效的。

How can I preserve these line endings, or what did jQuery replace the CRLF line-endings with?

如何保存这些行结束符,或者jQuery用什么来替换CRLF行结束符?

2 个解决方案

#1


1  

Using ajax, I've seen line breaks being posted as \r\n and \n depending on browser and environment, but in general, I found that checking for \n and replace it with <br /> is the safest bet.

使用ajax,我看到过根据浏览器和环境将换行符作为\r\n和\n发布,但通常,我发现检查\n并将其替换为
是最安全的选择。

For example in C#:

例如在c#中:

"Hello\nWorld".Replace("\n", "<br />");
or
"Hello\r\nWorld".Replace("\n", "<br />");

#2


1  

An alternative is to use the CSS white-space property to style your output.

另一种方法是使用CSS空白属性来样式化输出。

<div style="white-space: pre;">
    Hello
World
</div>

should be rendered as

应该显示为

    Hello
World

with tabs and linespacing preserved

保留制表符和衬线

See http://www.w3.org/TR/CSS2/text.html#white-space-prop for more info

更多信息请参见http://www.w3.org/TR/CSS2/text.html#白色空格-道具

#1


1  

Using ajax, I've seen line breaks being posted as \r\n and \n depending on browser and environment, but in general, I found that checking for \n and replace it with <br /> is the safest bet.

使用ajax,我看到过根据浏览器和环境将换行符作为\r\n和\n发布,但通常,我发现检查\n并将其替换为
是最安全的选择。

For example in C#:

例如在c#中:

"Hello\nWorld".Replace("\n", "<br />");
or
"Hello\r\nWorld".Replace("\n", "<br />");

#2


1  

An alternative is to use the CSS white-space property to style your output.

另一种方法是使用CSS空白属性来样式化输出。

<div style="white-space: pre;">
    Hello
World
</div>

should be rendered as

应该显示为

    Hello
World

with tabs and linespacing preserved

保留制表符和衬线

See http://www.w3.org/TR/CSS2/text.html#white-space-prop for more info

更多信息请参见http://www.w3.org/TR/CSS2/text.html#白色空格-道具