【问题标题】:Need help converting " to ' when using .val() [duplicate]使用 .val() 时需要帮助将“转换为”[重复]
【发布时间】: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=&quot;image/png&quot; data-trix-attachment=&quot;{&quot;contentType&quot;:&quot;image/png&quot;,&quot;filename&quot;:&quot;Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;,&quot;filesize&quot;:9291,&quot;height&quot;:77,&quot;href&quot;:&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;,&quot;url&quot;:&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;,&quot;width&quot;:305}&quot; href=&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;><figure class=&quot;attachment attachment-preview png&quot;><img src=&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot; height=&quot;77&quot; width=&quot;305&quot;><figcaption class=&quot;caption&quot;>Screen Shot 2015-11-16 at 11.36.45 AM.png <span class=&quot;size&quot;>9.07 KB</span></figcaption></figure></a>

jQuery 代码

   var new1 = $("#trix-input-1").val();

【问题讨论】:

  • data-trix-attachment的颜色编码,你需要把整个值变成一个字符串,把外面的"换成',这样就解决了你的问题。您无需进行&amp;quot; 转换。
  • "#trix-input-1" 没有出现在问题的html 处?
  • 转义引号有助于防止 XSS。请参阅en.wikipedia.org/wiki/… 了解更多信息。

标签: jquery html json regex


【解决方案1】:

replace 的语法错误,它是 replace(oldStr, newStr),而不是相反。见这里:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace

所以:

var e_encoded = new1.replace(/"/g, "&quot;"); 

应该是:

var e_encoded = new1.replace(/&quot;/g, "'"); 

你的例子很好用:

console.log('&lt;a data-trix-content-type=&amp;quot;image/png&amp;quot; data-trix-attachment=&amp;quot;{&amp;quot;contentType&amp;quot;:&amp;quot;image/png&amp;quot;,&amp;quot;filename&amp;quot;:&amp;quot;Screen Shot 2015-11-16 at 11.36.45 AM.png&amp;quot;,&amp;quot;filesize&amp;quot;:9291,&amp;quot;height&amp;quot;:77,&amp;quot;href&amp;quot;:&amp;quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&amp;quot;,&amp;quot;url&amp;quot;:&amp;quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&amp;quot;,&amp;quot;width&amp;quot;:305}&amp;quot; href=&amp;quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&amp;quot;&gt;&lt;figure class=&amp;quot;attachment attachment-preview png&amp;quot;&gt;&lt;img src=&amp;quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&amp;quot; height=&amp;quot;77&amp;quot; width=&amp;quot;305&amp;quot;&gt;&lt;figcaption class=&amp;quot;caption&amp;quot;&gt;Screen Shot 2015-11-16 at 11.36.45 AM.png &lt;span class=&amp;quot;size&amp;quot;&gt;9.07 KB&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;&lt;/a&gt;'.replace(/&amp;quot;/g, "'"))

用 document.write 代替控制台输出:

document.write('&lt;a data-trix-content-type=&amp;quot;image/png&amp;quot; data-trix-attachment=&amp;quot;{&amp;quot;contentType&amp;quot;:&amp;quot;image/png&amp;quot;,&amp;quot;filename&amp;quot;:&amp;quot;Screen Shot 2015-11-16 at 11.36.45 AM.png&amp;quot;,&amp;quot;filesize&amp;quot;:9291,&amp;quot;height&amp;quot;:77,&amp;quot;href&amp;quot;:&amp;quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&amp;quot;,&amp;quot;url&amp;quot;:&amp;quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&amp;quot;,&amp;quot;width&amp;quot;:305}&amp;quot; href=&amp;quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&amp;quot;&gt;&lt;figure class=&amp;quot;attachment attachment-preview png&amp;quot;&gt;&lt;img src=&amp;quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&amp;quot; height=&amp;quot;77&amp;quot; width=&amp;quot;305&amp;quot;&gt;&lt;figcaption class=&amp;quot;caption&amp;quot;&gt;Screen Shot 2015-11-16 at 11.36.45 AM.png &lt;span class=&amp;quot;size&amp;quot;&gt;9.07 KB&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;&lt;/a&gt;'.replace(/&amp;quot;/g, "'"))

【讨论】:

  • OP 根本不应该处理&amp;quot;,他们只需要正确地将值添加到他们的属性中。
  • @SpencerWieczorek 是的,当你写它并且不想重复它时,我已经赞成你的评论。刚刚直接回答了 OP 关于替换的问题,但我同意代码需要修复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-24
  • 2019-12-23
  • 2021-11-06
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多