special characters in id of html tags

In html code I am using code <input type = "text" id = "abc@def.com"> to get text now it need to get its value which is entered in text field. I am using jquery to do that:

$( 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 ?


Try escaping it:

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

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


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

$('[id=ID]')

or

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

so instead of

bidID = "#" + idVal + "bid"

you can try

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

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

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

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

链接地址: http://www.djcxy.com/p/88014.html

上一篇: 使用javascript getElementById的Firefox XUL工具栏问题

下一篇: html标记的id中的特殊字符