简体   繁体   中英

Retaining paragraphs with jquery.post

I am using a script to send messages to multiple users at the same time on a social media website.

The jquery.post snippet looks like this:

$.ajax({
        type : "POST",
        url : "socialmediawebsite.com",
        data : "to="+send_to+ "&subject="+title+"&message="+desc+"",
        success : function(msg) {
            console.log(send_to + " success");
        }
});

The variable "desc" contains the body of the message. This variable is given its value by a prompt in which the user will type whatever he wants the message to contain. "send_to" is an array of the names of the users you want to send the messages to. "title" is a variable which contains the title of the message.

The script works fine except for the fact that it is not possible to add paragraphs to the "desc" variable. Is there any way to make this work, while still using a prompt to input the data?

Here is the section of the code which puts up the prompt:

function show_inputForm_desc(){
var text = prompt("description?","example"); 
if (text!=null && text!="") { 
    desc = text;
    check_values();
}

I had found similar questions on this website regarding making paragraphs work in such a request, however, none of these used a prompt to give value to the variable.

Greetings

Unfortunately, there's no way to do so, as prompt is always single line . You'd need a different solution providing a textarea in a modal window.

You could adopt the convention that some special character means a new line. For example, using the pipe |

 function show_inputForm_desc(){ var text = prompt("description?","example"); if (text!=null && text!="") { desc = text.split("|").join('\\n'); console.log(desc); } } show_inputForm_desc(); 

Writing hello|man will log

hello
man

To the console.

However, I believe it better to perform this replacement on the receiver end. Just send the string, pipe separated, and replaces pipes with linebreaks when you deliver the message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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