javascript - 使用来自另一个表的数据动态地将行添加到表中-jQuery

标签 javascript jquery html

我的页面上有两个表格,如图所示。 enter image description here

我想做的是:

  1. 选择源表中的行
  2. 从Source Table中读取ID字段、Name字段的数据,以及radio button的值。
  3. 将这些数据形成为目标表的行,并将这些行附加到目标表中。

现在我使用两个数组来存储从源表读取的 ID 和名称,但我对如何为目标表形成一行感到有点困惑。

$('#btnLinkCase').click(function (event) {
var caseID = $('#caseID').text();
var linkedIDArr = []; //array list for selected id
var linkedNameArr = []; //array list for selected name
$('#linkCaseTable tbody tr td:nth-child(2)').each(function (index, el) { //store each paire of <tr> into array
    linkedIDArr.push(el);
});
$('#linkCaseTable tbody tr td:last-child').each(function (index, el) { //store each paire of <tr> into array
    linkedNameArr.push(el);
});
$.each(linkedCaseIDArr, function (index, val) {
    //whats next?
});
});

请查看所有源代码here

有什么想法吗?提前致谢

编辑: 如果选择多行,它们将使用单选按钮中的相同值。例如:选择两行并选中“类型 1”。在目标表中,它们的关系都是“Type 1”。我希望我已经解释了自己..

最佳答案

尝试:

$('button').click(function (event) {
    $("#sourceTable tr:gt(0)").each(function(){
        var th = $(this);
        console.log("dfg")
        if($(th).find("input[name='link-cbx']").is(":checked")){ //assuming ids are unique remove condition if you do notwant so
            var id = $(th).find("td:eq(1)").text();
            var name = $(th).find("td:eq(7)").text();
            var type = "Type "+$("input[name='linkType']:checked").val();
            if($("#targetTable:contains('"+id+"')").length < 1){            
                $("#targetTable").append("<tr><td>"+id+"</td><td>"+name+"</td><td>"+type+"</td></tr>");
            }
        }
    });
});

Updated fiddle here.

关于javascript - 使用来自另一个表的数据动态地将行添加到表中-jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20173612/

相关文章:

javascript - Kik 机器人 | Uncaught ReferenceError : require is not defined

javascript - 删除条目或更新为空白(即 "")到 JSON 文件

jquery - 将 RGB 值转换为 HTML 颜色代码?

javascript - jQuery 中的正则表达式用多行文本字段的 SharePoint ListView 上的链接替换模式

html - CSS 或 JS 使 select 不会向右流出屏幕

HTML所有元素的原生CSS显示值列表?

php - 在特定时间和日期网站上播放视频

javascript - 为什么在使用条件(三元)运算符的 javascript 中, "="与 "==="会得到不同的结果?

jQuery 查找仅具有部分引用 ID 的控件

html - 在 <td> 上设置宽度,在 <th> 下使用 colspan