I am creating a table row dynamically. Each row is having onclick
event. When I click on that table row I want to pass row object to a function but my problem is, while passing that object, I am getting [object object]
string. So I am not able to use that object in function.
Please give some solution thanks in advance.
This is my code:
var row;
$.each(mydata, function(i,data){
row+='<tr onclick="myfunction(\''+data+'\')"><td >data.name</td><td >data.age</td></tr>;
});
$("#myTable").append(row);
I would better use jQuery for defining click
event handlers. Here is your updated code:
var $table = $("#myTable");
$.each(mydata, function(i,row){
$tr = $('<tr>').appendTo($table);
$tr.on("click", myfunction);
});
Convert the data object to the following style string.
"{\"name\": \"lenient\", \"age\": 18}"
Then bind it to the click event, you will get the object as the parameter inside the click function.
尝试添加JSON.stringify()
onclick="myfunction("+JSON.stringify(data) +")"
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.