为什么 jQuery 添加反斜杠“\”我的字符串中的字符?

发布于 2024-10-06 04:57:09 字数 2117 浏览 6 评论 0原文

我使用 jQuery 将元素的内容替换为字符串,其中包含一些元素。以下是我用来创建 $value 字符串的代码:

var value =  '<span class="fn">' + $("#fn_").val() + '</span>';
$("input", this).val(value);

但是,实际输出的内容全部都有退格字符,如下所示:

<span class="\&quot;fn\&quot;">Name</span>

我假设这可能是我的一个愚蠢错误,但不确定我做错了什么?感谢任何建议!

编辑

为了清楚起见,这里是完整的代码,我正在为 jEditable 创建一个自定义输入类型,它允许我内联编辑 vcard。

$.editable.addInputType('person', {
element : function(settings, original) {     
    var fn  = $('<input id="fn_"/>');
    var bday  = $('<input id="bday_" />');
    var wday  = $('<input id="wday_" />');
    var dday  = $('<input id="dday_" />');
    var spouse  = $('<input id="spouse_" />');

    $(this).append(fn);
    $(this).append(bday);
    $(this).append(wday);
    $(this).append(dday);
    $(this).append(spouse);

    /* Hidden input to store value which is submitted to server. */
    var hidden = $('<input type="hidden">');
    $(this).append(hidden);
    return(hidden);
},

content : function(string, settings, original) {
        var $data = $(string);

        var data = {};
        $data.each(function () {
            var $t = $(this);
            data[$t.attr('class')] = {
                                   class: $t.attr('class'),
                                   value: $t.text()};
        });

        alert(data.length);

        $("#fn_", this).val('Name');
        $("#bday_", this).val('Born');
        $("#wday_", this).val('Married');
        $("#dday_", this).val('Died');
        $("#spouse_", this).val('Spouse');
    },

    submit: function (settings, original) {
    var value =  "<span class=fn>" + $("#fn_").val() + '</span>' + '<span class=spouse>' + $("#spouse_").val() + '</span>' + '<span class=bday>' + $("#bday_").val() + '</span>' + '<span class=wday>' + $("#wday_").val() + '</span>' +'<span class=dday>' + $("#dday_").val() + '</span>';
        $("input", this).val(value);
    }

});

I'm using jQuery to replace the contents of an element with a string, which contains some elements. The following is the code I'm using to create $value, the string:

var value =  '<span class="fn">' + $("#fn_").val() + '</span>';
$("input", this).val(value);

However, what actually gets outputted has backspace characters all through it, like this:

<span class="\"fn\"">Name</span>

I'm assuming this is probably a stupid mistake on my part, but not sure what I'm doing wrong? Appreciate any advice!

EDIT

For clarity, here's the full code, I'm creating a custom input type for jEditable that allows me to inline edit a vcard.

$.editable.addInputType('person', {
element : function(settings, original) {     
    var fn  = $('<input id="fn_"/>');
    var bday  = $('<input id="bday_" />');
    var wday  = $('<input id="wday_" />');
    var dday  = $('<input id="dday_" />');
    var spouse  = $('<input id="spouse_" />');

    $(this).append(fn);
    $(this).append(bday);
    $(this).append(wday);
    $(this).append(dday);
    $(this).append(spouse);

    /* Hidden input to store value which is submitted to server. */
    var hidden = $('<input type="hidden">');
    $(this).append(hidden);
    return(hidden);
},

content : function(string, settings, original) {
        var $data = $(string);

        var data = {};
        $data.each(function () {
            var $t = $(this);
            data[$t.attr('class')] = {
                                   class: $t.attr('class'),
                                   value: $t.text()};
        });

        alert(data.length);

        $("#fn_", this).val('Name');
        $("#bday_", this).val('Born');
        $("#wday_", this).val('Married');
        $("#dday_", this).val('Died');
        $("#spouse_", this).val('Spouse');
    },

    submit: function (settings, original) {
    var value =  "<span class=fn>" + $("#fn_").val() + '</span>' + '<span class=spouse>' + $("#spouse_").val() + '</span>' + '<span class=bday>' + $("#bday_").val() + '</span>' + '<span class=wday>' + $("#wday_").val() + '</span>' +'<span class=dday>' + $("#dday_").val() + '</span>';
        $("input", this).val(value);
    }

});

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

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

发布评论

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

评论(3

江城子 2024-10-13 04:57:09

嗯,我似乎通过使用

'<span class=fn>'

而不是

'<span class="fn">'

删除类名周围的引号来完成这项工作。由于某种原因,引号被正确地放回输出中,但是我无法使用多个类。

Hmm I seem to have got this working by using

'<span class=fn>'

instead of

'<span class="fn">'

i.e. removing the quotes from around the class name. For some reason the quotes are being put back in correctly in the output, however I'm not able to use multiple classes.

不必你懂 2024-10-13 04:57:09

如果您尝试设置跨度内的 HTML,一种正确的方法是:

.empty().append($('<span class="fn"/>').text($("#fn_").val()))

或者如果您想完全替换该元素:

.replaceWith($('<span class="fn"/>').text($("#fn_").val()))

或者如果您只想删除类 vcard,请添加类 fn,并设置内容:

.removeClass('vcard').addClass('fn').text($("#fn_").val())

这可确保文本框中的值在插入 span 元素之前正确进行 HTML 转义。使用 .val 设置元素的 HTML 内容是不正确的;为此,请使用 .html ,尽管在本例中没有必要。

If you are trying to set the HTML that is inside a span, one correct way to do it is:

.empty().append($('<span class="fn"/>').text($("#fn_").val()))

Or if you want to replace the element entirely:

.replaceWith($('<span class="fn"/>').text($("#fn_").val()))

Or if you just want to remove class vcard, add class fn, and set the contents:

.removeClass('vcard').addClass('fn').text($("#fn_").val())

This ensures that the value from the textbox is properly HTML-escaped before it is inserted into the span element. It's not correct to use .val to set the HTML contents of an element; use .html for that, although it is not necessary in this case.

善良天后 2024-10-13 04:57:09

您是否知道您的提交函数是在将数据发送到服务器之前执行的(查看 Jeditable 源代码),这意味着您所有的“跨度”都被发送到服务器,因此可能对您的元素进行编码的是您的服务器端脚本。

干杯。

Did you know that your submit function is executed before sending data to the server (take a look at Jeditable source), this means all your "spans" are sent to the server, so what is probably encoding your elements is your server-side script.

Cheers.

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