将数组对象传递给javascript函数

时间:2016-04-01 10:00:11

标签: javascript arrays sharepoint-2013

我正在创建动态链接,其中我传递带有一些值和数组的函数但是当调用该函数时,该数组变为字符串,如“[object Object]”。它不会保留为数组

Javascript Code

var mdata = {};
            mdata.itemId = data.d.ID.toString();
            mdata.sucessMsg = "Gate Pass detail document is uploaded successfully.";
            mdata.errorMsg = "Gate Pass detail document uploading cancel";
            tdUplDoc.html("<a id='uplDoc' href='#' onclick=\"javascript:openBasicDialog('UploadDocument.aspx','Upload CivilID or Passport Copy','" + mdata + "');\">Upload Documents</a>");

calling function

function openBasicDialog(tUrl, tTitle, tdata) {
    var options = {
        url: tUrl,
        title: tTitle,
        args: tdata,
        dialogReturnValueCallback: onPopUpCloseCallBackWithData
    };
    SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);
    return false;
}

我如何将数组保持为数组而不是字符串。我做错了什么。请有人帮助我:(

3 个答案:

答案 0 :(得分:1)

将其作为JSON字符串传递,

"'..,'" + JSON.stringify(mdata) + "');\">Upload Documents</a>");

解析后在函数内部使用它,

function openBasicDialog(tUrl, tTitle, tdata) {
    var options = {
        url: tUrl,
        title: tTitle,
        args: JSON.parse(tdata),  //parse it here
        dialogReturnValueCallback: onPopUpCloseCallBackWithData
    };

答案 1 :(得分:0)

这将无法执行以下操作

var a = document.createElement('a');
a.id = 'uplDoc';
a.href='#';
a.addEventListener('click', function(){
  openBasicDialog('UploadDocument.asp', 'Upload CivilID or Passport Copy', mdata);  
}
tdUplDoc.appendChild(a);

答案 2 :(得分:0)

您正在获取[object Object],因为您正在进行字符串连接,这是此对象的字符串表示形式:

"<a id='uplDoc' href='#' onclick=\"javascript:openBasicDialog('UploadDocument.aspx','Upload CivilID or Passport Copy','" + mdata + "');\"

我建议添加元素并在该元素上写一个click event listener

tdUplDoc.html("<a id='uplDoc' href='#'>Upload Documents</a>");

document.getElementById('uplDoc').addEventListener('click', function(){
   openBasicDialog('UploadDocument.asp', 'Upload CivilID or Passport Copy', mdata);  
});