ColdFusion - 如何将单引号输出到文本字段中?

发布于 2024-12-01 13:37:07 字数 1197 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一瞬间的火花 2024-12-08 13:37:07

单引号不会给 HTML 中的属性值带来问题,除非:
* 您没有引用属性值,例如:

<input value=#myvar#>

这里的解决方案是引用您的属性,例如:

<input value="#myvar#">


* 你正在引用你的属性,但使用的是单引号:

<input value='#myVar#'>

最终会是:

<input value='value with a ' in it'>

这-当然是无效的标记:浏览器将值视为“带有 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:

<input value=#myvar#>

The solution here is to quote your attributes, eg:

<input value="#myvar#">

or
* you are quoting your attributes, but are using single quotes :

<input value='#myVar#'>

Will end up being:

<input value='value with a ' in it'>

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.

埖埖迣鎅 2024-12-08 13:37:07

您遇到的问题与值字段的字符分隔符有关。如果您使用单引号作为字段分隔符,并且您的应用程序提供了单引号,则会出现问题。我过去处理这个问题的一种方法是在字段中使用双引号。下面显示的行应插入您的代码中:

LOCAL.Properties = " value=""#preserveSingleQuotes(ARGUMENTS.Value)#""";

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:

LOCAL.Properties = " value=""#preserveSingleQuotes(ARGUMENTS.Value)#""";

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!

宫墨修音 2024-12-08 13:37:07
// CREATE TEXT INPUT
function createInputBox(Value) {
    LOCAL.Properties = ' value="' &#preserveSingleQuotes(ARGUMENTS.Value)#& '"';
    LOCAL.Item = '<input size="50" type="text" #LOCAL.Properties# />';
    return LOCAL.Item;
}

// 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;
}

// USE EITHER STRING
MyString = "This string works fine.";
MyString = "This single quote's the problem!";
writeOutput(createInputBox(MyString));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文