2011-07-13 31 views
8

在html代碼中,我使用代碼 <input type = "text" id = "[email protected]">以獲取文本 現在需要獲取其輸入文本字段的值。我正在使用jQuery來做到這一點:html標記的id中的特殊字符

$(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); 
    }); 
}); 

通過這樣做,我得到的值錯誤未定義。 我試圖從id中刪除特殊字符以獲取其值。但我需要帶特殊字符的ID。 我如何使用jquery獲得它的值?

回答

0

在此有一個鏡頭,不是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); 
    }); 
}); 
24

而不是使用#爲ID嘗試,試試這個匹配屬性就是這樣,

$('[id=ID]') 

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

所以不是

bidID = "#" + idVal + "bid" 

你可以試試

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

感謝它爲我工作 – big

+6

你可能想在這種情況下,接受/給予好評:) – 2011-08-02 07:12:14

+0

在某些情況下,您可能需要這樣做:$(「[id ='ID']」) –