javascript - 为什么这个字符串不会转义?

标签 javascript python html escaping web2py

我们有一些带有 onSubmit 的 html:

<form id="login_box" onSubmit="alert('{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}'); this.submit(); this.reset(); return false;">

问题是无论我做什么它都不会转义嵌套引号。我试过:

<form id="login_box" onSubmit="alert('{{=T(\'Thank you for contacting us! We have received your email and will contact you shortly.\')}}'); this.submit(); this.reset(); return false;">

<form id="login_box" onSubmit="alert(\"{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}\"); this.submit(); this.reset(); return false;">

<form id="login_box" onSubmit=\"alert(\\"{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}\\"); this.submit(); this.reset(); return false;\">

以及我能想到的任何其他组合,即使我知道它不会起作用。我没有尝试转义嵌套引号。

最佳答案

我认为您在 python 中使用 twig 或任何类似的模板系统。

如果你想翻译{{=T('message to translate')}},只需将该字符串放入自定义属性即可。

示例:

<form id="login_box" msg="{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}" onSubmit="alert(this.getAttribute('msg')); this.submit(); this.reset(); return false;">

the example

:)

关于javascript - 为什么这个字符串不会转义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11039212/

相关文章:

html - 在 CSS 中使两个圆圈位于垂直线的顶部

javascript - 为什么我的 JavaScript 计算器不能使用 base.html?

javascript - 从 VBA 转换为 JS - 输出错误

javascript - jQuery 淡入和淡出消息列表

javascript - 如何以编程方式隐藏选择选项?

javascript - 纯 Javascript 生成的 Bootstrap4 Collapsible 不会展开或折叠

python - 为什么我的生成器函数总是返回相同的值?

python - 在子进程调用ffmpeg中禁用输出

python - 从 Python KeyError 异常中获取键名

html - CSS3背景随机切割我的图像