问题:
I'm using JavaScript to pull a value out from a hidden field and display it in a textbox. 我正在使用JavaScript从隐藏字段中提取值并将其显示在文本框中。 The value in the hidden field is encoded. 隐藏字段中的值被编码。
For example, 例如,
<input id='hiddenId' type='hidden' value='chalk & cheese' />
gets pulled into 被拉入
<input type='text' value='chalk & cheese' />
via some jQuery to get the value from the hidden field (it's at this point that I lose the encoding): 通过一些jQuery从隐藏字段中获取值(此时我失去了编码):
$('#hiddenId').attr('value')
The problem is that when I read chalk & cheese
问题是,当我阅读chalk & cheese
chalk & cheese
from the hidden field, JavaScript seems to lose the encoding. 从隐藏字段起, chalk & cheese
似乎失去了编码。 I do not want the value to be chalk & cheese
. 我不希望这个价值是chalk & cheese
。 I want the literal amp;
我想要文字amp;
to be retained. 被保留。
Is there a JavaScript library or a jQuery method that will HTML-encode a string? 是否有将对字符串进行HTML编码的JavaScript库或jQuery方法?
解决方案:
参考一: https://stackoom.com/question/57LA/从输入字段读取属性时-HTML编码丢失参考二: https://oldbug.net/q/57LA/HTML-encoding-lost-when-attribute-read-from-input-field
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/4333403