我想将数组发送到其他函数
<div id="test"></div>
<div id="test2"></div>
<input type="button" value="chk" id="go" />
<script>
$(function()
{
var c = 1;
var i = 5;
var data = [[]];
for ( c = 1 ; c <= i ; c++ )
{
data[c] = [];
data[c]['proDetails'] = 'asdf'+c;
data[c]['proDeCount'] = 'qwer'+c;
}
c=1;
for ( c = 1 ; c <= i ; c++ )
{
$('#test').text($('#test').text() + " " + data[c]['proDetails'] + " " + data[c]['proDeCount']);
}
$('#go').click(function()
{
test(data);
});
})
function test()
{
var c = 1;
var i = 5;
for ( c = 1 ; c <= i ; c++ )
{
$('#test2').text($('#test2').text() + " " + data[c]['proDetails'] + " " + data[c]['proDeCount']);
}
}
</script>
从此代码<div id="test"></div>
,它可以显示数据表单数组。
但<div id="test2"></div>
它无法显示数组中的数据。
它有一些错误“ReferenceError:数据未定义”
答案 0 :(得分:0)
在函数中声明它
function test(data)
{
if(typeof data != 'undefined')
{
var c = 1;
var i = 5;
for ( c = 1 ; c <= i ; c++ )
{
$('#test2').text($('#test2').text() + " " + data[c]['proDetails'] + " " + data[c]['proDeCount']);
}
}
}
你可以做的其他事情是全局变量如:
<script>
var data = [[]]; // <---- here
$(function()
{
var c = 1;
var i = 5;
for ( c = 1 ; c <= i ; c++ )
{
data[c] = [];
data[c]['proDetails'] = 'asdf'+c;
data[c]['proDeCount'] = 'qwer'+c;
}
c=1;
for ( c = 1 ; c <= i ; c++ )
{
$('#test').text($('#test').text() + " " + data[c]['proDetails'] + " " + data[c]['proDeCount']);
}
$('#go').click(function()
{
test(data);
});
})
function test()
{
var c = 1;
var i = 5;
for ( c = 1 ; c <= i ; c++ )
{
$('#test2').text($('#test2').text() + " " + data[c]['proDetails'] + " " + data[c]['proDeCount']);
}
}
</script>