0
我試圖將2d數組傳遞給函數。但是當我循環訪問setParams
中的數組時,數組中有一些額外的值(發生在switch
中的每case
),我不知道它們來自哪裏。如何通過值函數傳遞二維數組
function setParams(button, params) {
$mydialog = $("#dialog").dialog({
autoOpen: true,
buttons: {
"ok": function() {
$(this).dialog("close");
for(var key in params) {
var value = params[key].toString();
var arr = value.split(',');
alert(arr[0] + " " + arr[1]);
var control = $("#dialog").find('input[name='+arr[1]+']');
if (control.is(':checkbox'))
{
button.data('id')[arr[0]] = control.is(":checked");
}
else
{
button.data('id')[arr[0]] = control.val();
}
}
sendRequest(button);
},
"cancel": function() {
$(this).dialog("close");
}
}
});
}
菜單功能
$(function() {
$('#navigationMenu a').click(function(e) {
e.preventDefault();
e.stopPropagation();
var button = $(this);
var cl = button.attr("class");
console.log(button.attr('href'));
switch(cl) {
case "input-params":
$('.id').show();
$('.requestDate').hide();
$('.startEndDate').hide();
setParams(button, [["id","id"]]);
break;
case "input-params-location":
$('.id').show();
$('.requestDate').hide();
$('.startEndDate').hide();
setParams(button, [["locationId","id"]]);
break;
case "input-params-openinghours":
$('.id').show();
$('.requestDate').show();
$('.startEndDate').hide();
var myArray = [["locationId","id"],["requestDate","date"]];
setParams(button, myArray);
break;
case "input-params-allocations":
$('.id').hide();
$('.requestDate').hide();
$('.startEndDate').show();
setParams(button, [["startDate","startDate"],["endDate","endDate"],["includeGroupActivities","includeGroupActivities"]]);
break;
case "input-params-allocations-id":
$('.id').show();
$('.requestDate').hide();
$('.startEndDate').show();
setParams(button, [["id","id"],["startDate","startDate"],["endDate","endDate"],["includeGroupActivities","includeGroupActivities"]]);
break;
default:
sendRequest(button)
}
});
});
除我通過這些值的值是數組太
1)
function(){return v;
} undefined
2)
function Array() {[native code]} undefined
中
3)
function (i v)Array.forEach(this
我怎麼能傳遞沒有得到額外的值以正確的方式排列?
由於它是一個二維數組,你應該能夠得到像這樣的'params [0] [0]''的值。這將獲得第一個元素的第一個值。所以當你用'params [0] [0]'得到'[['hey','test],['hey1','test1']''時,你會得到值''hey'''。 – wouter140