简体   繁体   English

如何将复杂对象作为参数发送给javascript函数

[英]how to send complex object as parameter to a javascript function

I have a Html Table which displays data and is having delete and update functionality as below, 我有一个显示数据的HTML表格,并且具有以下删除和更新功能,

function DesignBTable(data) {
    $("#ExpTableBody tr").remove();
    var rowIndex = 0;
    $.each(data, function (index, value) {
        var fromDt = moment(value.from_date).format("MMM/YYYY");
        var toDt = moment(value.to_date).format("MMM/YYYY");

        $("#ExpTableBody").append("<tr>" +
            "<td>" + value.org_name + "</td>" +
            "<td>" + fromDt + "</td>" +
            "<td>" + toDt + "</td>" +
            "<td>" + value.designation + "</td>" +
            "<td>" + value.location + "</td>" +
            "<td>" + value.is_relevant + "</td>" +
            "<td>" + '<input type="button" value = "X" class="btn btn-danger btn-sm" onClick="Javacsript:deleteRow(\'' + value.exp_id + '\',\'' + value.from_date + '\')">' + '  ' +
             '<input type="button" value = "E" class="btn btn-info btn-sm" onClick="Javacsript:editRow(\'' + value + '\')">' + "</td>" +

            "</tr>");
        //alert(moment(value.from_date).format("MM/YYYY"));
        rowIndex++;
    });
};

The value object contains various fields. 值对象包含多个字段。 On the Click event of the delete button I send value.exp_id and value.from_date as parameter to deleteRow function. 在删除按钮的Click事件上,我将value.exp_id和value.from_date发送为deleteRow函数的参数。

I want to add Edit functionality where if I click on the edit button it should send the value as object so that I can access all the fields in it. 我想添加“编辑”功能,如果单击“编辑”按钮,它将把该值作为对象发送,以便我可以访问其中的所有字段。

When I try to send value as parameter to the JS function it errors out as undefined. 当我尝试将值作为参数发送给JS函数时,它错误地显示为未定义。

To create a string for code that uses the object, you would need to create a string representation of the object: 要为使用该对象的代码创建字符串,您需要创建该对象的字符串表示形式:

... onClick="editRow(' + JSON.stringify(value) + ')" ...

(Note: The JSON object is not supported in some older browsers.) (注意:某些旧版浏览器不支持JSON对象 。)

If you create elements directly instead of creating a string that you create elements from, you can bind the event directly and use the object without creating a string representation of it: 如果直接创建元素而不是创建用于创建元素的字符串,则可以直接绑定事件并使用对象,而无需创建该对象的字符串表示形式:

$("#ExpTableBody").append(
  $("<tr>").append([
    $("<td>").text(value.org_name),
    $("<td>").text(fromDt),
    $("<td>").text(toDt),
    $("<td>").text(value.designation),
    $("<td>").text(value.location),
    $("<td>").text(value.is_relevant),
    $("<td>").append([
      $("<input>", { type: "button", value: "X", className: "btn btn-danger btn-sm" }).click(function(){
        deleteRow(value.exp_id, value.from_date)
      }),
      $("<input>", { type: "button", value: "E", className: "btn btn-info btn-sm" }).click(function(){
        editRow(value);
      })
    ])
  ])
);

As a side effect, by using the text method to put the values in the cells, it's protected agains script injection. 副作用是,通过使用text方法将值放在单元格中,可以再次保护脚本注入。 If you actually have HTML code that you want to put in a cell, you would use the html method instead. 如果实际上有要放在单元格中的HTML代码,则应改用html方法。 That naturally means that you should encode it properly when you create that HTML code to protect against script injection. 这自然意味着您在创建该HTML代码时应正确编码,以防止脚本注入。


Side note: The javascript: pseudo protocol is only used when you put code in an URL, ie a href attribute. 旁注:仅当您将代码放在URL(即href属性)中时,才使用javascript:伪协议。 If you use it anywhere else it becomes a label instead. 如果在其他任何地方使用它,它将变成标签。 This is not harmful, but useless and confusing. 这不是有害的,但是没有用并且令人困惑。

the problem of 'value' accessibility is it's defined only inside the function not outside. “值”可访问性的问题是仅在函数内部定义而不在外部定义。

I see you use JQuery, so you can use JQuery.data to make a link between an html object and a custom value. 我看到您使用JQuery,因此可以使用JQuery.data在html对象和自定义值之间建立链接。 In your case you can add the value directly to the button at the end of the row. 您可以将值直接添加到行末尾的按钮上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM