2012-03-02 48 views
0

我正在嘗試將一個元素的id添加到數組中。但我收到錯誤 - Microsoft JScript運行時錯誤:對象不支持此屬性或方法。 下圖顯示了我如何將ID值添加到數組。爲什麼$(this).id的值未定義?將此this添加到JS數組中

enter image description here

感謝

+0

請發佈您的HTML – 2012-03-02 06:21:00

回答

4

你應該做的事:

 

var comments = []; 
//instead of 
var comments = {}; 
 

因此,這將是:

 

var comments = []; 
$("textarea.comment_wide_textbox_control").each(function() { 
    comments.push($(this).attr("id"), $(this).val()); 
}); 

0

我會嘗試$(this).attr('id');

+0

'this.id'工作得很好。沒有必要使用jQuery,它不增加任何價值,也不會解決問題。 – jfriend00 2012-03-02 06:42:44