繁体   English   中英

MVC视图JavaScript中的转义字符

[英]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 })'}" />&nbsp;

@item.name.ToString()有一个单引号( ' ),我需要对其进行转义。

当我查看原始HTML时, '&#39

如您在此答案中 @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 })'}" />&nbsp;

您可以使用@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.

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