简体   繁体   English

通过 onclick 事件将多个参数传递给 JavaScript 函数

[英]Pass multiple parameters via onclick event to JavaScript function

I've rendered a table using Jquery's DataTable library.我已经使用 Jquery 的 DataTable 库呈现了一个表。 Within this datatable I've rendered hyperlink anchors with an onclick event that calls a javascript function.在此数据表中,我使用调用 javascript 函数的 onclick 事件呈现超链接锚点。 I need to pass more than one parameter to these functions.我需要向这些函数传递多个参数。

I'm able to pass one parameter to the function but I'm unable to pass the variables as two separate parameters.我可以将一个参数传递给函数,但我无法将变量作为两个单独的参数传递。 See below code.见下面的代码。 Eg The value of the GuestID parameter in the emailGuest function takes both GuestID and email eg 84a09eeb-9d8d-43cb-9719-e597fc5ad215,someone@site.com*例如,emailGuest 函数中的 GuestID 参数值同时采用 GuestID 和电子邮件,例如 84a09eeb-9d8d-43cb-9719-e597fc5ad215,someone@site.com*

DataTable hyperlink render section数据表超链接呈现部分

                    data: null, render: function (data, type, row )
                    {
                        return "<a href='#' class='btn btn-danger' onclick= DeleteData('" + row.id + "'); >Delete</a>";
                    }
                },
                {
                    data: null, render: function (data, type, row) {
                        return "<a href='#' class='btn  btn-default' onclick= emailGuest('" + row.id + ',' + row.email + "'); >Email</a>";
                    }
                },
            ]

        });
    });

emailGuest Function emailGuest 函数

   function emailGuest(GuestID,email) {
        $('#GuestEmail').modal('show');
        document.getElementById("emailID").value = GuestID;

        document.getElementById("emailAddress").value = email;

    }

Your piece of code wrote both variables in one because you missed some ' in there ( ', ' changed to "', '" ):您的一段代码将两个变量合二为一,因为您错过了其中的一些'', '更改为"', '" ):

              data: null, render: function (data, type, row )
                {
                    return "<a href='#' class='btn btn-danger' onclick= DeleteData('" + row.id + "'); >Delete</a>";
                }
            },
            {
                data: null, render: function (data, type, row) {
                    return "<a href='#' class='btn  btn-default' onclick= emailGuest('" + row.id + "', '" + row.email + "'); >Email</a>";
                }
            },
        ]

    });
});

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

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