0

这是我的要求,我有一个包含日期数据的数组,例如

$.each(dateWiseArr, function(index, currentDate){
    myDateArray[currentDate] = "myData";
    dateWiseOtherId[currentDate] = "otherId";
});
alert(myDateArray+"::"+dateWiseOtherId);//working fine and showing right data
$("#myBtn").attr("onClick", "myPassingFunction("+myDateArray+", "+dateWiseOtherId+")");

function myPassingFunction(myDateArray, dateWiseOtherId){
    alert(myDateArray[0]);//throw undefined
}

现在,当我按下按钮函数调用成功但变量变为字符串时。

如果您需要更多理解,请告诉我。

注意:我的日期

4

3 回答 3

1

绑定函数而不是绑定字符串,则不需要将值转换为字符串:

$("#myBtn").click(function(){
  myPassingFunction(myDateArray, dateWiseOtherId);
});
于 2012-11-07T12:46:05.550 回答
1

下面试试。它应该工作。

$.each(dateWiseArr, function(index, currentDate){
    myDateArray[currentDate] = "myData";
    dateWiseOtherId[currentDate] = "otherId";
});

alert(myDateArray+"::"+dateWiseOtherId);//working fine and showing right data

$("#myBtn").click(function (event) {
     myPassingFunction(myDateArray, dateWiseOtherId);
});

function myPassingFunction(myDateArray, dateWiseOtherId){
    alert(myDateArray[0]);//throw undefined
}
于 2012-11-07T12:49:08.633 回答
0

绑定事件的正确方法:检查这个工作小提琴http://jsfiddle.net/vC4Yk/

var myDateArray = ["some data"];
var dateWiseOtherId = ["some other data"];

alert(myDateArray+"::"+dateWiseOtherId);//working fine and showing right data

$("#myBtn").click(function(){
   myPassingFunction(myDateArray, dateWiseOtherId);      
});

function myPassingFunction(myDateArray, dateWiseOtherId){
    alert(myDateArray[0]);
}   ​
于 2012-11-07T12:55:44.990 回答