如何将引号传递给javascript函数

发布于 2024-12-23 09:41:25 字数 391 浏览 1 评论 0原文

我有一个 javascript 函数,它接受包含引号的字符串并显示在输入字段中。但这个函数不接受引号后的字符串

function searchFormatter(cellvalue, options, rowObject) {
    return '<input id="txt' + cellvalue + '" type="text"  disabled="disabled"  value="' + cellvalue + '" />';
}

Eg。 21" 英寸 ==> 21

无论如何,我可以将引号传递给函数并打印它们。我不想替换它们。我想显示 他们。

I have a javascript function which accepts strings which include quotation marks and displays in a input field. But this function does not accept the string after the quotation mark

function searchFormatter(cellvalue, options, rowObject) {
    return '<input id="txt' + cellvalue + '" type="text"  disabled="disabled"  value="' + cellvalue + '" />';
}

Eg. 21" Inch ==> 21

Is there anyway I can pass quotation marks to the function and print them. I don't want to replace them. I want to display them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

向地狱狂奔 2024-12-30 09:41:25

您必须转义特殊 HTML 字符,例如 "<>
例如:

function searchFormatter(cellvalue, options, rowObject) {
    cellvalue = cellvalue.
                replace(/&/g,'&').
                replace(/>/g,'>').
                replace(/</g,'<').
                replace(/"/g,'"');
    return '<input id="txt' + cellvalue + '" type="text"  disabled="disabled"  value="' + cellvalue + '" />';
}

You must escape special HTML characters like ", <, >.
Eg:

function searchFormatter(cellvalue, options, rowObject) {
    cellvalue = cellvalue.
                replace(/&/g,'&').
                replace(/>/g,'>').
                replace(/</g,'<').
                replace(/"/g,'"');
    return '<input id="txt' + cellvalue + '" type="text"  disabled="disabled"  value="' + cellvalue + '" />';
}
苏辞 2024-12-30 09:41:25

您必须传递 cellValue 的值,如下所示

var cellValue = 21"(应为 21 后跟 " 后跟 ;(分号))

您可以在输入框中获取 21" 形式的值。
希望这有帮助。

You have to pass the value of the cellValue as shown below
(
var cellValue = 21"(Should be given as 21 followed by " followed by ;(semicolon))

You can get the value as 21" in the input box.
Hope this helps.

趁微风不噪 2024-12-30 09:41:25

您需要将 " 替换为 "

使用此辅助方法:

function htmlEncodeInputDisplay(string) {
    if (string == null || jQuery.trim(string) == "") return string;
    string = string.replace(/</g, "<");
    string = string.replace(/>/g, ">");
    string = string.replace(/&/g, "&");
    string = string.replace(/ /g, " ");
    string = string.replace(/\"/g, """);
    return string;
}

You need to replace " with &quot;

Use this helper method:

function htmlEncodeInputDisplay(string) {
    if (string == null || jQuery.trim(string) == "") return string;
    string = string.replace(/</g, "<");
    string = string.replace(/>/g, ">");
    string = string.replace(/&/g, "&");
    string = string.replace(/ /g, " ");
    string = string.replace(/\"/g, """);
    return string;
}

只需在创建输入元素时对值进行转义,然后对值进行转义即可正确显示。

简化示例:

function make_input(val) {
    return '<input value="' + escape(val) + '" />';
}

// add to dom

$('input').each(function(){
   var current_value = $(this).val()
   var new_value = unescape(current_value)
   $(this).val(new_value)
})

just escape the value when creating the input element, and then unescape the value to display it correctly.

Simplified example:

function make_input(val) {
    return '<input value="' + escape(val) + '" />';
}

// add to dom

$('input').each(function(){
   var current_value = $(this).val()
   var new_value = unescape(current_value)
   $(this).val(new_value)
})
回忆凄美了谁 2024-12-30 09:41:25

您可以使用 Javascript 函数 escape() 对字符串进行编码,然后根据需要使用 unescape() 对其进行解码。

您还可以手动转义它们,如下例所示。

var name = prompt("Please write your \'name\' in the box\r Write it with \"quotes\" like this.","");

使用 \ 转义的引号作为字符串的一部分通过。

You can use the Javascript Functions escape() to encode a sting and then unescape() to decode them if needed.

You can also manually escape them like in the example below.

var name = prompt("Please write your \'name\' in the box\r Write it with \"quotes\" like this.","");

The quote escaped using \ goes through as a part of the string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文