从输入字段读取属性时,HTML编码丢失

爷,独闯天下 提交于 2020-08-16 19:08:42

问题:

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 &amp; cheese' />

gets pulled into 被拉入

<input type='text' value='chalk &amp; 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 &amp; cheese 问题是,当我阅读chalk &amp; cheese chalk &amp; cheese from the hidden field, JavaScript seems to lose the encoding. 从隐藏字段起, chalk &amp; 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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!