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
Add a ment  | 

4 Answers 4

Reset to default 4

You have to escape it with html equivalent when you create input.

<input type="text" id="secondBox" value="' + termText.replace(/\"/g,"&quot;") + '">

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="&quot;Test&quot;">

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