用于动态表生成的可单击行..帮帮我

Clickable rows for dynamic table generation...help me out

本文关键字:单击 动态 用于      更新时间:2023-09-26

我正在向动态表中添加元素。我面临的问题是,我需要生成的行是可点击的,通过它我可以导航到一个新的网页。

这是我的代码:

//adding rows dynamically after fetching data from table
function addRow() {
        var myName = document.getElementById("name");
        var age = document.getElementById("age");

        var table = document.getElementById("myTableData");
        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);
       // row.insertCell(0).innerHTML= '<input type="button" value = "Delete" onClick="Javacsript:deleteRow(this)">';
//this is where I fetch the value for the dynamic rows
        row.insertCell(0).innerHTML=myName.value;
        row.insertCell(1).innerHTML= age.value;

    }

我正在使用:

$('#tablename').on('click','tr',function(){alert('hello');});

但它对我不起作用

问题在于以下

$('#table-name').on('click', 'tr', function() {alert('hello');});

id不正确,请尝试

$('#myTableData').on('click', 'tr', function() {alert('hello');});

参见工作小提琴