jQuery即使使用引号也无法正确读取该值

时间:2019-06-26 08:49:23

标签: jquery

所以我在数据库列中有此值:

\u25cf10:00-10:50 IELTS Listening Randy  change to \r\n(1) 09:00-09:50 Rose or (2)  11:00-11:50 Zel\r\n\u25cf16:00-16:50 Ed IELTS  Reading \r\n(1) 15:00-15:50 Jane or (2) 13:00-13:50 Rose\r\n\r\nMy class schedule on the system is wrong~ please kindly check it, thank you very much!\r\n\r\n

此值来自名为Remark的列

因此,我将使用JQuery post数据显示数据并获取每个列的值,其他列也很好,但是当我进入Remark时,它显示如下:enter image description here

这是怎么发生的?我尝试过replace,但仍然不适合我

2 个答案:

答案 0 :(得分:4)

问题是由换行符-\r\n引起的。解决此问题的快速方法是使用模板文字,因为它们可以应付换行符:

$('[name="form-remarks"]').val(`●10:00-10:50 IELTS Listening Randy  change to 
(1) 09:00-09:50 Rose or (2)  11:00-11:50 Zel
●16:00-16:50 Ed IELTS  Reading 
(1) 15:00-15:50 Jane or (2) 13:00-13:50 Rose

My class schedule on the system is wrong~ please kindly check it, thank you very much!

`);
textarea {
  width: 600px;
  height: 120px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea name="form-remarks"></textarea>

但是请注意,这在IE中不起作用。

或者,您需要将换行符替换为更合适的字符,例如空格。

答案 1 :(得分:1)

以下代码应能正常工作,我使用反引号代替了引号

var data = `\u25cf10:00-10:50 IELTS Listening Randy  change to \r\n(1) 09:00-09:50 Rose or (2)  11:00-11:50 Zel\r\n\u25cf16:00-16:50 Ed IELTS  Reading \r\n(1) 15:00-15:50 Jane or (2) 13:00-13:50 Rose\r\n\r\nMy class schedule on the system is wrong~ please kindly check it, thank you very much!\r\n\r\n`

$('[name="form-remarks"]').val(data)
console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input name="form-remarks"/>