在onclick事件传递参数时逃脱撇号 [英] Escape apostrophe when passing parameter in onclick event

查看:251
本文介绍了在onclick事件传递参数时逃脱撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递的公司名称到一个onclick事件。有些公司的名字在他们撇号。我说'.Replace(',')'到COMPANY_NAME领域。这使得onclick事件火,但确认消息显示为简&安培;#39; S焊接公司。

I'm passing the company name to an onclick event. Some company names have apostrophes in them. I added '.Replace("'", "'")' to the company_name field. This allows the onclick event to fire, but the confirm message displays as "Jane&# 39;s Welding Company".

<a href="#" onclick="return Actionclick('<%= Url.Action("Activate", new {id = item.company_id}) %>', '<%= Html.Encode(item.company1.company_name.Replace("'", "&#39;")) %>');" class="fg-button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh"></span></a>

<script type="text/javascript">
function Actionclick(url, companyName) 
{
    if (confirm('This action will activate this company\'s primary company ('+companyName+') and all of its other subsidiaries.  Continue?')) 
    {
        location.href = url;
    };
};

修改
确认消息显示&放大器;#39;在消息,而不是'。当我键入​​它在这里,它取代了&放大器;#39;以'。新增的空间,这样就不会发生。我想知道将它传递给我的onclick事件,也没有适当做多替换邮件中显示出来(如果有更好的方法)的最佳方式。

EDIT The confirm message shows the &# 39; in the message rather than the '. When I typed it out here, it replaced the &# 39; with a '. Added spaces so that wouldn't happen. I want to know the best way to pass it to my onclick event and also properly display it in the message without doing multiple replaces (if there is a better way).

推荐答案

有两种选择,因为我看到了。

There are two options as I see it.


  1. 如果你用引号()的参数,而不是撇号/单引号('),那么你不应该需要逃避它。HTML编码将编码任何引号护理(如果他们是。在字符串)和撇号的将不会是一个问题,虽然,因为JavaScript是已经包裹在引号,你将需要反斜线您的报价如:

  1. If you wrap the parameters in quotes (") instead of apostrophes/single quotes (') then you shouldn't need to escape it at all. HTML encoding will take care of encoding any quotes (if they are in the string) and the apostrophe's won't be a problem. Though, as the javascript is already wrapped in quotes, you will need to backslash escape your quotes. eg:

的onclick =返回Actionclick(\\&LT;%= Url.Action(激活,新{ID = item.company_id})%GT; \\,\\&LT;%= Html.En code(item.company1.company_name)%GT; \\);

反斜线的公司名称,因为它是只需要撇号逃脱,而不是HTML最终的JavaScript字符串。例如:

Backslash escape the company name as it's only the final javascript string that needs the apostrophe escaped, not the HTML. eg:

的onclick =返回Actionclick('&LT;%= Url.Action(激活,新{ID = item.company_id})%GT;','&LT;%= Html.En code(item.company1.company_name.Replace(',\\\\))%GT;');

这篇关于在onclick事件传递参数时逃脱撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆