需要帮助转换“ to'when using using .val()[复制]

时间:2021-09-01 02:04:10

This question already has an answer here:

这个问题在这里已有答案:

I am using jquery to get the value of a hidden input. The hidden input contains a data-trix-attachment attribute with JSON object. The problem is that jquery replaced the quotes with " . I am not sure why.

我使用jquery来获取隐藏输入的值。隐藏输入包含带有JSON对象的data-trix-attachment属性。问题是jquery用“"”替换了引号。我不知道为什么。

I have looked around for a solution, and can't seem to find one.

我四处寻找解决方案,似乎无法找到解决方案。

I tried doing a regex to replace the " with ' but that didn't work either. (Below is an example)

我试着做一个正则表达式来取代“ '但这也没有用。 (下面是一个例子)

Can anyone please help me solve this. Below is the code I tried using.

任何人都可以帮我解决这个问题。以下是我尝试使用的代码。

    var new1 = $("#trix-input-1").attr("value");
   var e_encoded = new1.replace(/"/g, """); 

HTML Code

<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 Response

<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 Code

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

1 个解决方案

#1


2  

Wrong syntax for replace, it's replace(oldStr, newStr) not the other way round. See here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace

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

So this:

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

should be:

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

You example works fine:

你的例子很好:

console.log('<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>'.replace(/&quot;/g, "'"))


With document.write instead of console output:

使用document.write而不是控制台输出:

document.write('<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>'.replace(/&quot;/g, "'"))

#1


2  

Wrong syntax for replace, it's replace(oldStr, newStr) not the other way round. See here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace

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

So this:

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

should be:

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

You example works fine:

你的例子很好:

console.log('<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>'.replace(/&quot;/g, "'"))


With document.write instead of console output:

使用document.write而不是控制台输出:

document.write('<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>'.replace(/&quot;/g, "'"))