简体   繁体   English

服务器端单引号打破javascript字符串

[英]Server side single quote breaking javascript string

I have a server side variable 我有一个服务器端变量

string test = "test";
ticks = "['" + test + "']";

I am setting ticks to a hidden field on the page but when I try to get the value in javascript the single quote is breaking the string and all i am getting from it is [. 我正在为页面上的隐藏字段设置刻度,但是当我尝试在javascript中获取值时,单引号会破坏字符串,而我从中获取的是[。 Is there a way around this? 有没有解决的办法?

I am using c# 3. 我正在使用c#3。

Thanks 谢谢

不要手动形成字符串,而是使用json解析器

string js = new JavaScriptSerializer().Serialize(new[]{"a","b","c"});

尝试使用'\\''转义测试值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM