【发布时间】:2015-11-16 17:43:58
【问题描述】:
我正在使用 jquery 来获取隐藏输入的值。隐藏的输入包含一个带有 JSON 对象的 data-trix-attachment 属性。问题是 jquery 用 " 替换了引号。我不知道为什么。
我四处寻找解决方案,但似乎找不到。
我尝试使用正则表达式将 " 替换为 ' 但这也不起作用。 (下面是一个例子)
谁能帮我解决这个问题。下面是我尝试使用的代码。
var new1 = $("#trix-input-1").attr("value");
var e_encoded = new1.replace(/"/g, """);
HTML 代码
<a contenteditable="false" href="http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png" data-trix-attachment="{"contentType":"image/png","filename":"Screen Shot 2015-11-16 at 11.36.45 AM.png","filesize":9291,"height":77,"href":"http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png","url":"http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png","width":305}" data-trix-content-type="image/png" data-trix-id="38">
jQuery 响应
<a data-trix-content-type="image/png" data-trix-attachment="{"contentType":"image/png","filename":"Screen Shot 2015-11-16 at 11.36.45 AM.png","filesize":9291,"height":77,"href":"http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png","url":"http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png","width":305}" href="http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png"><figure class="attachment attachment-preview png"><img src="http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png" height="77" width="305"><figcaption class="caption">Screen Shot 2015-11-16 at 11.36.45 AM.png <span class="size">9.07 KB</span></figcaption></figure></a>
jQuery 代码
var new1 = $("#trix-input-1").val();
【问题讨论】:
-
看
data-trix-attachment的颜色编码,你需要把整个值变成一个字符串,把外面的"换成',这样就解决了你的问题。您无需进行&quot;转换。 -
"#trix-input-1"没有出现在问题的html处? -
转义引号有助于防止 XSS。请参阅en.wikipedia.org/wiki/… 了解更多信息。