【问题标题】:here i am creating table row by giving dynamically data for table data . But i didn't get the data by their id在这里,我通过为表数据提供动态数据来创建表行。但我没有通过他们的 id 获得数据
【发布时间】:2018-10-02 08:43:22
【问题描述】:

在这里,我通过为表格数据提供动态数据来创建表格行。但我没有通过他们的 id 获得数据

if(xmlhttp.status == 200)
                        {
                          var adminList = xmlhttp.responseJSON;
                          var outputStr = '';
                          for(var i = 0; i < adminList.length; i++)
                          {
                            outputStr += '<tr><td id="tdusername">' + adminList[i].username + '</td><td id="tdfullname">' + adminList[i].fullName + '</td><td id="tdemail">'+ adminList[i].email+'</td><td id="tdedit">'+adminList[i].id+'</td></tr>';
                          }
                          $('#admin_list tbody').html(outputStr);
            else
                    {
                      $('#msg').html(xmlhttp.status + ": " + xmlhttp.statusText);
                    }

【问题讨论】:

  • 有什么问题?
  • 生成表格后,我如何通过他们的 id 获取特定的表格数据?我动态地给出了每个 td 的 id,但它不起作用@GeorgeBailey
  • 您正在为所有人分配相同的 ID。 ID 必须是唯一的。可以使用id="tdusername"+i等,如果这个函数只执行一次
  • 不,它不工作。我正在使用这个 $('#tdedit').click(adminInfo);通过单击表中的数据来调用该函数,但它不起作用。 @GeorgeBailey
  • $('#tdedit').val().toString() 这是我用来获取数据的

标签: javascript jquery html ajax web


【解决方案1】:

如你所说,你是点击数据来获取特定行数据,那么我建议给&lt;tr&gt;添加一个id

 var outputStr ='<table>';
 for(var i = 0; i < adminList.length; i++)
 {
  outputStr += '<tr id="tdedit'+adminList[i].id+'" onclick="showdata('+adminList[i].id+')"><td id="tdusername">' + adminList[i].username + '</td><td id="tdfullname">' + adminList[i].fullName + '</td><td id="tdemail">'+ adminList[i].email+'</td><td id="tdedit">'+adminList[i].id+'</td></tr>';
 }
outputStr +='</table>';

function showdata(id){
  var row = document.getElementById('tdedit'+id);
  //now you can access all column by their index
  var username=row.cells[0].innerHTML;
  var fullname=row.cells[1].innerHTML;
  var email=row.cells[2].innerHTML;
}

【讨论】:

  • 否 仍然无法正常工作。显示“Uncaught ReferenceError: showdata is not defined at HTMLTableRowElement.onclick”@crack_iT
  • 您必须在任何其他 JavaScript 函数之外添加我提供的 showdata 函数
  • 你能告诉我如何使用或获取单元格[0],单元格[1]的值。它在控制台“Uncaught TypeError: Cannot read property '0' of undefined at showdata (script.js:93) at HTMLTableRowElement.onclick ((index):1)”@crack_iT
  • 你能不能 console.log(Id, row);在 showdata() 中评论后告诉我结果
  • 在给出console.log(id,row) 后,它会显示相应的行click 1 w.fn.init [tr#tdedit1] 5 w.fn.init [tr#tdedit5] 4 w。 fn.init [tr#tdedit4]
猜你喜欢
  • 1970-01-01
  • 2021-02-20
  • 1970-01-01
  • 1970-01-01
  • 2022-08-09
  • 2016-06-16
  • 1970-01-01
  • 1970-01-01
  • 2014-10-15
相关资源
最近更新 更多