问题
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