我正在尝试将数组作为参数传递给函数
见 -->小提琴<--
根据我的测试,该函数似乎将参数作为字符串而不是数组读取。如何将数组作为参数传递给函数?
HTML
<button id='arr1'>Click for Array 1</button>
<button id='arr2'>Click for Array 2</button><br/><br/>
<div id='mainstuff'>
<p>When you click button, text should be added</p>
</div>
jQuery
$(document).ready(function() {
var arr1 = ["one", "two", "three"];
var arr2 = ["eleven", "twelve", "thirteen", "fourteen"];
$('button').click(function() {
var myval = $(this).attr('id');
showstuff(myval);
});
function showstuff(myval) {
var x = "<p>The new text is " + myval[2] + "</p>";
$('#mainstuff').append(x);
}
});
编辑:小提琴已更新以修复语法错误。