通过单选按钮选择一行时,将一行数据复制到同一页面的文本字段中

Copy a row of data to the textfields of same page on selecting a row through radio button

本文关键字:一行 一页 字段 文本 选择 单选按钮 数据 复制      更新时间:2023-09-26

通过jquery 动态生成行

$("#showbtn").on("click", function() {
                var tblRow;
                $.getJSON('multimob.action', $("#mobileno").serialize(), function(data) {
                    if (data.mobileno === null) {
                        alert("Wrong Mobile No.,please Check");
                    } else {
                        $.each(data.ls, function(index, value) {
                            var count = index;
                            tblRow = $("<tr><td> <input type='radio' id='chk' name='chk' onclick='copyrecord('" + count + "');'/></td><td id='fidrow" + count + "'>" + value.id + "</td>"
                                    + "<td>" + value.name + "</td>" + "<td>"
                                    + value.mobileno + "</td>" + "<td>" + value.addres + "</td></tr>");
                            alert(count + tblRow.toString());
                            $("#farmTable tbody").append(tblRow);
                        });
                    });

数据行是动态创建的,一旦我选择单选按钮,它就应该从该行中获取数据,并且必须将其复制到同一页面中的文本字段中。

帮我把这东西扔掉。

试试这个例子

使用类

<input type='radio' id='chk' name='chk' class="chk"/>

HTML

$(document).on('click','.chk',function(){
var text=[];
$(this).parents('tr').find('td:not(:first)').each(function(){
   console.log($(this).text()) 
   text.push($(this).text())     
});
$('#name').val(text[0]);
$('#no').val(text[1]);
$('#add').val(text[2])
});

演示

根据您的评论:我可以将id和名称更改为"chk"+count+"之类的。但是,如何在通过单选按钮选择行时复制行数据

你可以这样得到:

$("#farmTable").on('change', '[id^="chk"]', function(){
    var tblVals = $("#farmTable tr").map(function(){
       return $(this).find('td:not(:first)').text();
    });
    console.log(tblVals);
});