开发者

Putting quotes into a form field using Javascript

This must be so simple but I'm having a brain freeze.

I have a input field :

<input value="">

I want to populate the value using javascript and the var has quotes " in it. For example, when I populate the value on page load with PHP, I'll just swap " for &quot; (or htmlentities, etc).

But when you do the same with js then it just displays &quot; (obviously).

开发者_如何学编程Can I just write out " without escape ?


You need to escape the " character in the Javascript string:

var str = "I have a \"!";
//Or
var str = 'I have a "!';

You can then assign this string to the value property, or to any other property.


For a pair of double quotes, put them inside single quotes.

<input value='""' />

Or to set via javascript

document.forms[0].elements[0].value = '""';


If you use single quotes around your value it should work:

<input type="text" value='quo"tes' />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜