ASP.NET, javascript: silly annoying apostrophe problem

巧了我就是萌 提交于 2019-12-23 03:51:27

问题


I wanted to add a confirmation box to my ImageButton, so I have the following:

btn.Attributes.Add("onclick", "return confirm('OK to delete this district from the consultant\'s list?');");

But then .NET html-encodes the whole thing so it comes out in the HTML as:

onclick="return confirm('OK to delete this district from the consultant's list?');" 

so the apostrophe in "consultant's" looks the same as the apostrophes surrounding the confirm parameter, and javascript gets all confused, and the confirm box does not come up.

Deleting the "consultant's" apostrophe solves the problem, but then I have an even more annoying punctuation error which is unacceptable.

What I seem to need is a "javascript" escape for the apostrophe, something that will escape the apostrophe only for javascript, not for HTML .. if that makes sense.

Any ideas??


回答1:


btn.Attributes.Add("onclick", "return confirm(\"OK to delete this district from the consultant\'s list?\");");

Escape " and use confirm("...")



来源:https://stackoverflow.com/questions/4834461/asp-net-javascript-silly-annoying-apostrophe-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!