[英]Escape character in MVC view JavaScript
我的页面上有这行:
<input type="button" title="Join" value="Join" onclick="if(confirm('Are you sure you want to join @item.name.ToString() ?')) {location.href='@Url.Action("JoinTeam", "Team", new { id = item.id })'}" />
@item.name.ToString()
有一个单引号( '
),我需要对其进行转义。
当我查看原始HTML时, '
是'
。
如您在此答案中 @Html.Raw(jsString)
在您的情况下使用@Html.Raw(jsString)
。
<input type="button" title="Join" value="Join" onclick="if(confirm('Are you sure you want to join @Html.Raw(item.name.ToString()) ?')) {location.href='@Url.Action("JoinTeam", "Team", new { id = item.id })'}" />
您可以使用@Json.Encode
来转义特殊字符:
<input type="button" title="Join" value="Join"
onclick="if(confirm('Are you sure you want to join @Json.Encode(item.name) ?')){location.href='@Url.Action("Home", "Index", new { id = item.id })'}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.