简体   繁体   English

使用JavaScript(onblur)替换双引号的C#表单生成器-与单引号内的2个双引号冲突

[英]C# form builder using JavaScript (onblur) to replace double quotes - conflict with 2 double quotes within single quotes

I'm modifying a custom form builder app written in C# by adding onblur to replace problematic characters like <> /\\ and hopefully " . I can get all replaces to work except for the double quotes due to my 2 double quotes issue. Can anyone help me get this to work? 我正在修改一个用C#编写的自定义表单生成器应用,方法是添加onblur来替换有问题的字符,例如<> /\\并希望是" 。由于我的2双引号问题,双引号除外,我可以使所有替换工作。任何人都可以吗帮助我使它工作吗?
Here's a bit of my code: 这是我的一些代码:

this.Controls.Add(new LiteralControl(String.Format(@"   
... lots of html ...
<input name=""txtTextLabel"" id=""txtTextLabel"" type=""text"" onblur=""this.value = this.value.replace('<', '[').replace('>', ']').replace('double quotes', '');""/>
... lots more html ...
"));

The below works for me: 以下对我有用:

<input name="txtTextLabel" id="txtTextLabel" type="text" 
onblur='this.value = this.value.replace("<", "[").replace(">", "]").replace("\"", "");'/>

I've put a \\ before the double quote because that's how you escape the double quote in Javascript. 我在双引号前加上了\\,因为这是您在Javascript中转义双引号的方式。

Note that in the onblur attribute I've reversed your usage of the single and double quote in order for it to work. 请注意,在onblur属性中,为了使它起作用,我已经颠倒了您对单引号和双引号的使用。

Also I realise that in your code you're replacing < with [ and > with ] 我也意识到在您的代码中,您用<和[用>替换了]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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