我生成了自定义复选框,用于选择或取消选择我的数据。我有2个自定义函数用于选择和取消选择数据。在select方法中,我需要传递2个变量,它是字段值。
我的代码:
print
问题:
名称字段包含引号('')等特殊字符。以下是样本数据;
<span custom-checkbox="true"
is-checked="false"
name="id"
data-val="{this.id}"
on-select="DataSelect('{this.id+'\',\'' +this.name}')"
on-unselect="DataUnselect('{this.id}')" id="{this.id}">
</span>
function DataSelect(id, value){
console.log(id);
console.log(value);
//my code
}
function DataUnselect(id, value){
//my code
}
由于报价(&#39;),我在这里得到了这个问题。所以数据被操纵为;
id = "1";
name= "hey'len";
所以我在参数列表&#34;之后得到了#34; Uncaught SyntaxError:missing)。
请帮我纠正这个
答案 0 :(得分:2)
您可以使用.replace()
id = "1";
name= "hey'len";
name.replace('\'', '\\\'');
答案 1 :(得分:1)
id = "1";
name= "hey'len";
name.replace('\''g, '\\\'');
使用它将替换(')的所有出现。它也适用于“hey'len'new'something”。
试一试
答案 2 :(得分:0)
如果替换字符串具有特殊字符,则必须在创建RegExp之前替换它,如下所示:
var html = `
<div>This is Html</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's m</div>
<p>asdasdasally unchanged. It was popularised in the 1960s with the release of Letraset sheets <span>
<img src="https://via.placeholder.com/200x100?w=100" />
the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it </span>
to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
`;
var oldImage = 'https://via.placeholder.com/200x100?w=100';
var newImage = 'https://via.placeholder.com/350x150?w=1000';
var fixOldImageToRegExp = oldImage.replace(/\//g, '\\/').replace(/\?/g, '\\?').replace(/\w/g, '\\w');
var re = new RegExp(fixOldImageToRegExp,'gi');
document.write(html.replace(re, newImage));
这正在运行codepen