简体   繁体   中英

JSON object not getting passed in function

I am dynamically creating a html table based on the JSON response from an ajax call as shown below. I want to associate clickable event with some rows which would allow me to do some post processing on those json objects. However, I am not able to pass the json object through the function call as it is not picking it up. Could you please help me here. If I pass a property of the json object, I am able to achieve this but I want the whole object to be passed.

        $.get(url, { param: ID},
            function(data){                   

                $.each(data, function(index, product) {                                               
                   $('<tr>').appendTo($body)   
                    .append($('<td>').text(product.ID))        
                    .append($('<td>').text(product.firstName))      
                    .append($('<td>').text(product.lastName))
                    .append($('<td>').text(product.email))        
                    .append($('<td>')
                    .append($('<a href=\"javascript:deleteProduct(' + product.ID + ');\">')
                    .append($('<img src=\"/images/delete.png\">'))))
                    .append($('<td>')
                    .append($('<a href=\"javascript:editProduct(' + product + ');\">')
                    .append($('<img src=\"/images/edit.png\">'))));

                });

I am not able to invoke the editProduct(product) func call here however, deleteProduct(id) works.

Thanks.

product is an object which you are trying to put as a string, which means the function will look like this: editProduct(Object[object]) or similar.

Quick solution would be to use a stringify-like method before putting it in the string.

But a better solution would be to remove the inline js, make the elements before apppending them, use the .click() function to set the functionality instead, then append them to the tr.

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