传递数组或对象以将其转换为字符串

时间:2012-11-07 12:41:02

标签: javascript jquery

这是我的要求,我有一个数据,如日期数据,如

$.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
}

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

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

注意:myDate

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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
}

答案 2 :(得分: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]);
}   ​