简体   繁体   中英

Pass multiple parameters via onclick event to JavaScript function

I've rendered a table using Jquery's DataTable library. Within this datatable I've rendered hyperlink anchors with an onclick event that calls a javascript function. 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*

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

   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>";
                }
            },
        ]

    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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