简体   繁体   English

JSON对象未在函数中传递

[英]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. 我正在基于ajax调用的JSON响应动态创建html表,如下所示。 I want to associate clickable event with some rows which would allow me to do some post processing on those json objects. 我想将clickable事件与一些行相关联,这将允许我对那些json对象进行一些后期处理。 However, I am not able to pass the json object through the function call as it is not picking it up. 但是,我无法通过函数调用传递json对象,因为它没有拾取它。 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. 如果我传递json对象的属性,则可以实现,但是我希望整个对象都可以传递。

        $.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. 我无法在此处调用editProduct(product)函数调用,但deleteProduct(id)可以工作。

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. product是要作为字符串放入的对象,这意味着该函数将如下所示:editProduct(Object [object])或类似的对象。

Quick solution would be to use a stringify-like method before putting it in the string. 快速解决方案是在将其放入字符串之前使用类似于stringify的方法。

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. 但是更好的解决方案是删除内联js,在附加元素之前制作元素,使用.click()函数设置功能,然后将其附加到tr。

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

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