Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

jquery get component with value having double quotation

So the following code causes

value = '"hello!"'
$(`button[value="${value}"]`)

Error: Syntax error, unrecognized expression: button[value=""hello!""]

I understand this is caused by the value having the double quote. However, the value is set dynamically and we accept value to have double quotes, single quotes... Is there a way to solve this problem using perhaps other syntax?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to escape the quotes so that they become " instead of "

value = '"hello!"'
// Escaping double quotes
value = value.replace(/"/g, '"');
// Escaping single quotes
value = value.replace(/'/g, "'");
$(`button[value="${value}"]`)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...