本文介绍了javascript-如何在onclick事件中将数组作为参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组想要传递给javascript onclick函数.我在按钮的onclick事件中发送该数组.onclick函数返回我[object Object].有另一种方法可以用数组调用该函数吗?
I have an array that I want to pass to a javascript onclick function. I send that array in the onclick event of a button. The onclick function return me [object Object]. Is there a different way to do call that function with the array?
这里是我的代码:
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);
});
}
推荐答案
注意,在 js
处, data_type
似乎不是 Array
; data_type
似乎是 Object
Note, data_type
not appear to be Array
at js
; data_type
appear to be Object
尝试利用 .on()
,在 click
事件处理程序中调用 showABC()
,并以 data_type
作为参数
Try utilizing .on()
, calling showABC()
within click
event handler with data_type
as parameter.
var data_playtype = [];
data_playtype[1] = JSON.stringify({
"1": "apple",
"2": "orange",
"23": "berry",
"8": "grape",
"9": "mango"
});
var data_type = $.parseJSON(data_playtype[1]);
//{"1":"apple","2":"orange","23":"berry","8":"grape","9":"mango"}
var str1 = [];
str1.push("<td><a href=javascript:void(0)>Data</a></td>");
function showABC(fruit) {
$.each(fruit, function(pid, pt) {
alert(pt);
});
};
$("table tbody").append(str1).find("a")
.on("click", function() {
showABC(data_type)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<tbody></tbody>
</table>
这篇关于javascript-如何在onclick事件中将数组作为参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!