简体   繁体   English

保存动态生成的值 <td> 到清单

[英]Saving Values from a Dynamically Generated <td> to a List

I have pulled some values from an API which I have populated in a table. 我已经从表格中填充的API中提取了一些值。 On each table row I have dynamically added a save button. 在每个表格行中,我都动态添加了一个保存按钮。 I simply want to use the save button to save one value in a table cell (the first) to a list which will then either be saved to local storage or a database. 我只想使用保存按钮将表单元格(第一个)中的一个值保存到列表中,然后将其保存到本地存储或数据库中。

Here is my code: 这是我的代码:

jQuery(function ($) {
        $(document).on('click', '#save-btn', function () {
            console.log("Clicked!");
            mytr = $(this).closest("tr"), 
                mytd = mytr.find("td:first-child");
            $.each(mytd, function () {
                console.log($(this).text());
                $('.saved-list').append(`'<li>'`+mytd+`'</li>'`); 
            });
        });
    });

I can output to the console correctly, but I can't append the value to my list. 我可以正确输出到控制台,但是不能将值附加到列表中。 I'm getting: 我越来越:

[object Object]

Hopefully this is enough information to assist me. 希望这些信息对我有帮助。

instead of 代替

$('.saved-list').append(`'<li>'`+mytd+`'</li>'`); 

Do:- 做:-

$('.saved-list').append(`'<li>'`+$(this).text()+`'</li>'`); 

Note:- You need to add text there while you are adding object there.( mytd is object not text) 注意:-在其中添加对象时,需要在其中添加文本。( mytd是对象而非文本)

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

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