我的页面上有两个表格,如图所示。
我想做的是:
- 选择源表中的行
- 从Source Table中读取ID字段、Name字段的数据,以及radio button的值。
- 将这些数据形成为目标表的行,并将这些行附加到目标表中。
现在我使用两个数组来存储从源表读取的 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>");
}
}
});
});
关于javascript - 使用来自另一个表的数据动态地将行添加到表中-jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20173612/