admin管理员组文章数量:1419573
I have the following variable -
termText = $("#termBox").val();
where $("#termBox").val()
is equal to - "\"Test\""
I then am creating an html object and want to set its value to termText like -
<input type="text" id="secondBox" value="' + termText + '">
However this is not working. The odd thing is when $("#termBox").val()
does not have double quotes in it i.e. "Test"
(in javascript debugger is adding the quotes to show it is a string), this works fine and populates secondBox
value perfectly.
I dont want to escape double quotes from the string as they are required as there has to be a definition between when a user enters "Term"
or Term
however this is not working for double quotes.
How can I do this?
I have the following variable -
termText = $("#termBox").val();
where $("#termBox").val()
is equal to - "\"Test\""
I then am creating an html object and want to set its value to termText like -
<input type="text" id="secondBox" value="' + termText + '">
However this is not working. The odd thing is when $("#termBox").val()
does not have double quotes in it i.e. "Test"
(in javascript debugger is adding the quotes to show it is a string), this works fine and populates secondBox
value perfectly.
I dont want to escape double quotes from the string as they are required as there has to be a definition between when a user enters "Term"
or Term
however this is not working for double quotes.
How can I do this?
Share Improve this question asked Mar 30, 2015 at 14:36 EbikeneserEbikeneser 2,37213 gold badges60 silver badges119 bronze badges 3- You can use something else other than quotes. – Huangism Commented Mar 30, 2015 at 14:39
- the quotes are required, if a user enters double quotes around the string, then they have to be present in the secondBox val as stated. – Ebikeneser Commented Mar 30, 2015 at 14:41
-
I understand it is needed to indicate something, what I mean is since you are having this issue, can you use a different indicator? I am not sure if
value=""term""
will work – Huangism Commented Mar 30, 2015 at 14:46
4 Answers
Reset to default 4You have to escape it with html equivalent when you create input.
<input type="text" id="secondBox" value="' + termText.replace(/\"/g,""") + '">
I am not sure why you are allowing your users to enter delimiters, but set the value with .val()
instead:
var input = $('<input type="text" id="secondBox"/>').val(termText);
This way jQuery takes care of any delimiting required.
Escaping the quotes will result in raw quotes when accessing the value.
Console:
var termText = "\"Test\"";
var input = document.createElement('input');
input.setAttribute('value', termText);
console.log(input.value);
console.log(input.outerHTML);
Output:
"Test"
<input value=""Test"">
You should not allow your users to enter double, single quotes or back slashes. They are a few of the dangerous characters that you should be scrubbing form user enter fields. if you need the quotes latter you should add them backing yourself behind the scenes. You will be opening yourself up for Cross Site Scripting (XSS) attacks.
You could achieve what you are looking to do by just taking the user input; cleaned of dangerous characters; and them just pass that to the value of the new input. Just as TrueBlueAussie mentioned. With a little revision.
var termText = $("#termBox").val();
var input = $('#secoundBox').val(termText);
本文标签: javascriptCannot set val when string in double quotesStack Overflow
版权声明:本文标题:javascript - Cannot set val when string in double quotes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745315647a2653141.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论