ColdFusion - 如何将单引号输出到文本字段中?
我正在使用 ColdFusion 9。
我找不到成功将单引号输出到文本字段的方法。
我使用 CFSCRIPT 用户定义的函数创建表单字段。 (为了简单起见,我已最小化此示例的选项。)
当我的输出包含单引号时,文本字段会完全混乱,请务必运行该示例并查看 HTML。我尝试过使用 PreserveSingleQuotes() 各种可能的方式。
// USE EITHER STRING
MyString = "This string works fine.";
MyString = "This single quote's the problem!";
writeOutput(createInputBox(MyString));
// CREATE TEXT INPUT
function createInputBox(Value) {
LOCAL.Properties = " value='#preserveSingleQuotes(ARGUMENTS.Value)#'";
LOCAL.Item = "<input size='50' type='text' #LOCAL.Properties# />";
return LOCAL.Item;
}
你知道解决办法吗? ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++ 解答
摆脱preserveSingleQuotes() 函数,因为它在SQL 块之外不执行任何操作。 (谢谢亚当!)。
LOCAL.Properties = " value='#ARGUMENTS.Value#'";
然后,去掉单引号并替换为转义双引号:
LOCAL.Properties = " value=""#ARGUMENTS.Value#""";
但这仍然会被这样的字符串阻塞:
MyString = "This is my F#@'''""$":""ing problem!";
所以,添加 htmlEditFormat() 函数,如下所示:
LOCAL.Properties = " value=""#htmlEditFormat(ARGUMENTS.Value)#""";
感谢您的帮助!
I am using ColdFusion 9.
I can't find a means to successfully output a single quote into a text field.
I create form fields using a CFSCRIPT user defined function. (I've minimized the options for the sake of simplicity for this example.)
When my output contains a single quote, the text field gets totally screwed up, be sure to run the example and view the HTML. I have tried using PreserveSingleQuotes() every conceivable way possible.
// USE EITHER STRING
MyString = "This string works fine.";
MyString = "This single quote's the problem!";
writeOutput(createInputBox(MyString));
// CREATE TEXT INPUT
function createInputBox(Value) {
LOCAL.Properties = " value='#preserveSingleQuotes(ARGUMENTS.Value)#'";
LOCAL.Item = "<input size='50' type='text' #LOCAL.Properties# />";
return LOCAL.Item;
}
Do you know of a solution?
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ANSWER
Get rid of the preserveSingleQuotes() function, as it does nothing outside of a SQL block. (Thanks Adam!).
LOCAL.Properties = " value='#ARGUMENTS.Value#'";
Then, get rid of the single quotes and replaced with escaped double quotes:
LOCAL.Properties = " value=""#ARGUMENTS.Value#""";
This will still choke on strings like this though:
MyString = "This is my F#@'''""$":""ing problem!";
So, add the htmlEditFormat() function like this:
LOCAL.Properties = " value=""#htmlEditFormat(ARGUMENTS.Value)#""";
Thanks for the help!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
单引号不会给 HTML 中的属性值带来问题,除非:
* 您没有引用属性值,例如:
这里的解决方案是引用您的属性,例如:
或
* 你正在引用你的属性,但使用的是单引号:
最终会是:
这-当然是无效的标记:浏览器将值视为“带有 a 的值”,其余部分只是垃圾。
如果您需要这样做:
* 切换到使用双引号分隔符
* 在变量值周围使用 htmlEditFormat() (这将转义嵌入的双引号)。
要解决此类问题,请务必查看 HTML 源代码。这将帮助您弄清楚发生了什么。
注意:对于每个提到 keepSingleQuote() 的人:该函数在 CFQUERY 块之外不执行任何操作。所以这在这里没有帮助。
A single quote should not give you a problem in an attribute value in HTML, unless:
* you're not quoting the attribute values, eg:
The solution here is to quote your attributes, eg:
or
* you are quoting your attributes, but are using single quotes :
Will end up being:
This - of course is invalid mark-up: the browser sees the value as 'value with a ', and the rest of it is just garbage.
If you need to do this:
* switch to using double-quote delimiters
* use htmlEditFormat() around your variable value (this will escape embedded double-quotes).
To troubleshoot this sort of thing, ALWAYS look at the HTML source. This will help you work out what's going on.
NB: to everyone mentioning preserveSingleQuote(): this function does NOTHING outside of a CFQUERY block. So it's not going to help here.
您遇到的问题与值字段的字符分隔符有关。如果您使用单引号作为字段分隔符,并且您的应用程序提供了单引号,则会出现问题。我过去处理这个问题的一种方法是在字段中使用双引号。下面显示的行应插入您的代码中:
Sean Kimball 所示的方法同样有效。根据情况,我使用了这两种方法。
还有另一个评论:preserveSingleQuotes。我不能说我在数据库调用之外使用过这个,但如果它在这种情况下对你有用,我也学到了一些东西!
The issue you're experiencing relates to the character delimiters for the value field. If you use single quotes as field delimiters, and a single quote is provided by your app, there will be a problem. One ways I've dealt with this in the past is to use double quotes for the field. The line shown below should plug into your code:
The approach shown by Sean Kimball is equally valid. Depending on the situation, I've used both approaches.
There was another comment re: preserveSingleQuotes. I can't say that I've used this outside of database calls, but if it works for you in this situation, I've learned something, too!