【发布时间】:2017-02-07 15:36:13
【问题描述】:
我正在使用文本区域输入文本并在提交表单时,视图中显示的文本未保留任何换行符和空格。
文本区域:
<textarea type="text" id="topicDetails"></textarea>
尝试使用以下替换文本:
postTopic(){
var content = document.getElementById('topicDetails').value;
// textcontent = content.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ "" +'$2');
// textcontent = content.replace("\r\n", "\\r\\n");
// textcontent = content.replace(/\r?\n/g, '<br />');
// textcontent = content.replace(/\r?\n/g, '
');
// var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
// textcontent = (content + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
textcontent = content.replace(/\n/g,"<br>")
var topic = {
"topicId" : new Date().getTime(),
"title": document.getElementById('title').value,
"details": textcontent,
"username": DataMixin.data.username,
"userImage": "assets/img/Logos Divine Light/6.png",
"dayPosted": new Date().toLocaleString()
}
console.log('posting blog..... ', topic);
self.data.blogTopicsArr.push(topic);
$.ajax({
url: "/new_topic",
type: "POST",
data: JSON.stringify(self.data.blogTopicsArr),
contentType: "application/json",
success: function (res) {
console.log('res is ', res);
if (res == 'Authentication failed'){
self.data.blogTopicsArr.splice( - 1, 1);
self.update(self.data.blogTopicsArr);
riot.route("signup_popup");
} else if (res == 'saved'){
console.log('blog posted successfully: ', self.data.blogTopicsArr);
document.getElementById('title').value = '';
document.getElementById('topicDetails').value = '';
self.update();
} else if (typeof res.redirect == 'string'){
console.log('res.redirect ', res.redirect);
riot.route(res.redirect)
}
},
error: function (err) {
console.log('err>>>>', err);
}
});
$('#myModal').modal('hide');
}
尝试了三种不同的方法,但没有成功。
第三种方法给出带有<br /> 标签的输出。如何保留新行?
输出是:
Lorem Ipsum is simply dummy text.<br /> Industry's standard text ever since the 1500s, <br /><br />Why do we use it?<br />It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. <br /><br />
更新:
表单发布成功后,我正在使用 riot 的 self.update(); 或 this.update() 更新视图
更新2
实际上,我正在将表单数据发送到数据库并从数据库中取回文本以呈现它。但是发送到数据库的文本在保存到数据库之前插入了<br>标签,所以为什么它显示文本为Lorem Ipsum is simply dummy text.<br /> Industry's standard text ever since the 1500s, <br />??
【问题讨论】:
-
请告诉我们您想要达到什么目标?
-
@ZakariaAcharki 如标题中所述,我想在文本中保留新行和空格。我正在使用 textarea 输入文本并在提交时,不会保留空格或新行
-
@ÁlvaroGonzález 我只是将表单发布到服务器。我正在使用 riotjs。我猜这是不是 riotjs 的问题?
-
愚蠢的问题,但你尝试过吗:postTopic(){ var content = document.getElementById('topicDetails').value; var topic = {“详细信息”:内容,}}
-
@ÁlvaroGonzález 好的,我现在用更多代码更新问题,以便您了解。我只是在表单发布后更新视图
标签: javascript jquery html textarea riot.js