javascript html 编码 <textarea> 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11901455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
html encode <textarea> value
提问by Hello-World
How do I get the latest updated typed in value of a textarea and html encode the value.
如何获取最新更新的输入文本区域的值并对值进行 html 编码。
To get the value I use:
要获得我使用的价值:
$('textarea').val(); // this works cross browser
But if the value was "sdfgsdgdsgsdg <br/>"
how do I HTML encode it?
但是如果值是"sdfgsdgdsgsdg <br/>"
我如何对它进行 HTML 编码?
It breaks when I stringify()
it
当我stringify()
它时它会破裂
var e = { "d" : $('textarea').val()};
var s = JSON.stringify(e); //it breaks here
采纳答案by Ricardo Alvaro Lohmann
回答by Quentin
it break here
它在这里打破
That isn't a very helpful description of the error, but I'm guessing you mean I get an empty object.
这不是对错误的非常有用的描述,但我猜你的意思是我得到了一个空对象。
Spell textarea
correctly (it doesn't have an e
in the middle), so that you assign the value of the textarea instead of undefined
(which is what you get when you call val()
on an empty jQuery object).
拼写textarea
正确(e
中间没有 ),以便您分配 textarea 的值而不是undefined
(这是您调用val()
空 jQuery 对象时得到的值)。
This has nothing to do with HTML encoding. HTML isn't involved in this process at all. You take user input from a DOM, and then put it into JSON. JSON.stringify takes care of all your encoding needs.
这与 HTML 编码无关。HTML 根本不参与此过程。您从 DOM 获取用户输入,然后将其放入 JSON。JSON.stringify 可以满足您所有的编码需求。