我有一個數組,我想傳遞給一個javascript onclick函數。我在按鈕的onclick事件中發送該數組。 onclick函數返回我[對象對象]。有沒有一種不同的方式來與數組調用該函數?Javascript-如何在onclick事件傳遞數組作爲參數?
這裏我的代碼:
var data_type= $.parseJSON(data_playtype[1]);//{"1":"apple","2":"orange","23":"berry","8":"grape","9":"mango"}
str1.push('<td><a href="javascript:void(0)" onClick="showABC(\''+data_type+'\')">'Data'</td>');
function showABC(fruit){
$.each(fruit,function(pid,pt){
alert(pt);
});
}
不要使用HTML和內聯事件處理程序。使用jQuery構建DOM元素並綁定事件處理程序,以便您可以使用閉包。我強烈建議閱讀jQuery教程:https://learn.jquery.com/using-jquery-core/manipulating-elements/#creating-new-elements,https://learn.jquery.com/events/event-基礎知識/ –
'pt'是一個對象,所以當你執行'alert(pt)'時,它將對象串化爲'[object Object]'。嘗試訪問該對象的特定鍵。 –
@JoshBeam:在構建HTML時,字符串轉換早已發生。 –