简体   繁体   English

Textarea .val通过Ajax发送数据时没有换行符

[英]Textarea .val No Line Breaks when Sending Data via Ajax

I have seen a ton of articles about how to add line breaks to the .val, but none of them seem to be working for me. 我已经看过很多关于如何在.val中添加换行符的文章,但它们似乎都不适合我。 I have tried the jQuery recommended workaround: 我尝试过jQuery推荐的解决方法:

$.valHooks.textarea = {
  get: function( elem ) {
  return elem.value.replace( /\r?\n/g, "\r\n" );
  }
};

but, no matter what I seem to do, the text still gets smashed together into one line. 但是,无论我做什么,文本仍然会被拼凑成一行。

Here is my code: 这是我的代码:

HTML: HTML:

 <div id="email-info">
    <div id="email-top-row">
        <div id="email-subject-container">
            <label for="email-subject" class="email-labels">Subject:</label>
            <input id="email-subject"></input>
        </div>
        <div id="email-from-container">
            <label for="email-from" class="email-labels">From:</label>
            <input id="email-from"></input>
        </div>
    </div>

    <div id="email-body-container">
        <label for="email-body" class="email-body">Body:</label>
        <textarea id="email-body"></textarea>
    </div>
    <button id="email" onclick="emailSend()">Send Email</button>
</div>

Javascript: 使用Javascript:

function emailSend() {
 $.valHooks.textarea = {
  get: function( elem ) {
  return elem.value.replace( /\r?\n/g, "\r\n" );
  }
 };

 var emailSubject = $('#email-subject').val();
 var emailFrom = $('#email-from').val();
 var emailBody = $('#email-body').val();        

 emailURL = emailURL + '&email_subject=' + emailSubject;
 emailURL = emailURL + '&email_from=' + emailFrom;
 emailURL = emailURL + '&email_body=' + emailBody;

 $.getJSON(emailURL, function(data) {
    console.log(data);
 });
}

Now, say I add this to the textbox: 现在,假设我将其添加到文本框中:

test line 1

test line 2

test line 3

When I do a console.log of that, it comes out in the console correct with the line breaks, but in the network inspector tab, and on our server, what is sent is test line 1test line 2test line 3 . 当我执行一个console.log时,它在控制台中出现了正确的换行符,但是在网络检查器选项卡中,在我们的服务器上,发送的是test line 1test line 2test line 3

I am kinda at a loss of how to add in the line breaks to the url. 我有点不知道如何在网址中添加换行符。 I have seen a lot of stuff about the '\\n' but I have never seen that in my url or in my console logs. 我看过很多关于'\\ n'的内容,但我从来没有在我的网址或控制台日志中看到过。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you very much. 非常感谢你。

you need to escape your values coming from your textbox before appending to your url: 在附加到您的网址之前,您需要从文本框中删除您的值:

var emailSubject = encodeURIComponent($('#email-subject').val());
var emailFrom = encodeURIComponent($('#email-from').val());
var emailBody = encodeURIComponent($('#email-body').val());

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

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