jQuery html标签id中的特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6675454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:13:17  来源:igfitidea点击:

special characters in id of html tags

jqueryhtml

提问by big

In html code I am using code <input type = "text" id = "[email protected]">to get text now it need to get its value which is entered in text field. I am using jquery to do that:

在 html 代码中,我使用代码 <input type = "text" id = "[email protected]">来获取文本,现在它需要获取在文本字段中输入的值。我正在使用 jquery 来做到这一点:

$( document ).ready( function() {
    $( ".bid" ).click( function() {
        idVal = this.id
        bidID = "#" + idVal + "bid"
        spanID = "#" + idVal + "cbid"
        bidValue = $(bidID).val();

        alert(idVal)
        alert(bidID)
        alert(bidValue) //this alert says undefined
        $( spanID ).html( '&nbsp;' ).load( '{% url online_machines %}?id=' + idVal + '&bidvalue=' + bidValue);
    });
});

By doing this i get the value error undefined. I tried to remove special characters from the id to get its value. But I need the id with special characters. How can I get its value using jquery ?

通过这样做,我得到了未定义的值错误。我试图从 id 中删除特殊字符以获取其值。但我需要带有特殊字符的 id。如何使用 jquery 获取其值?

回答by

Instead of trying for ID using #, try this to match attribute like this,

与其尝试使用 # 获取 ID,不如尝试使用此方法来匹配这样的属性,

$('[id=ID]')

or

或者

$('[id='+ID+']')

so instead of

所以而不是

bidID = "#" + idVal + "bid"

you can try

你可以试试

bidID = "[id=" + idVal + "bid]"

回答by ysrb

Try escaping it:

尝试逃避它:

$('#abc\@def.com').val();

First paragraph of http://api.jquery.com/category/selectors/

http://api.jquery.com/category/selectors/的第一段

回答by kappamaki

It looks like JQuery has a useful method for this as of version 3 called escapeSelector.

从版本 3 开始,JQuery 似乎有一个有用的方法,称为escapeSelector.

$('#' + $.escapeSelector(my_id_with_special_chars));

$('#' + $.escapeSelector(my_id_with_special_chars));

https://api.jquery.com/jQuery.escapeSelector/

https://api.jquery.com/jQuery.escapeSelector/

回答by user3840288

$(document.getElementById('[email protected]'))also works.

$(document.getElementById('[email protected]'))也有效。

回答by daryl

Have a shot at this, not 100% on whether this is what you're after. Let me know:

试一试,而不是 100% 确定这是否是您所追求的。让我知道:

$( document ).ready(function() {
    $(".bid").click(function() {
        idVal = $(this).attr('id');
        bidID = "#" + idVal + "bid";
        spanID = "#" + idVal + "cbid";
        bidValue = bidID.val();

        alert(idVal);
        alert(bidID);
        alert(bidValue);
        spanID.html('&nbsp;').load('{% url online_machines %}?id=' + idVal + '&bidvalue=' + bidValue);
    });
});

回答by Joseph Jojo John

Use the function CSS.escape() to select that DOM element. In your case for the id [email protected] you can use the below code.

使用函数 CSS.escape() 选择该 DOM 元素。在您的 id [email protected] 的情况下,您可以使用以下代码。

$("#" + CSS.escape('[email protected]'));