Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击html表格单元格时如何获取其内部的数据?_Javascript_Jquery_Html Table - Fatal编程技术网

Javascript 单击html表格单元格时如何获取其内部的数据?

Javascript 单击html表格单元格时如何获取其内部的数据?,javascript,jquery,html-table,Javascript,Jquery,Html Table,我创建了一个html表并动态填充它。因此,我希望在单击表的单元格时,对该单元格中的数据发出警报。这是表格的代码 JSP 您可以使用$(this).text()获取表格单元格中的数据。 实际上,您正试图提醒“td”对象,而不是来自该对象的值,因此您可以使用以下方法来解决查询。由于动态添加了数据,因此我使用on而不是直接单击函数 var choice = $('table#load-opt tr td'); choice.on('click', function(){ alert($(this)

我创建了一个html表并动态填充它。因此,我希望在单击表的单元格时,对该单元格中的数据发出警报。这是表格的代码

JSP

您可以使用
$(this).text()
获取表格单元格中的数据。

实际上,您正试图提醒“td”对象,而不是来自该对象的值,因此您可以使用以下方法来解决查询。由于动态添加了数据,因此我使用on而不是直接单击函数

var choice = $('table#load-opt tr td');
 choice.on('click', function(){
 alert($(this).text());
});
使用
$(this).text()
使用警报($(this))而不是警报(choice),变量选项在此范围内未定义。
var fillTable = function($element, data) {
    var t_head = $("<tr></tr>");
    for(var ind=0; ind<data.length; ind++) {
        //name is the name and desc is the description for each option
        var $option = $("<tr></tr>");
        $option.html("<td>"+name+"</td><td>"+desc+"</td>");
    }
};
var choice = $('table#load-opt tr td');
choice.click(function(){
    alert(choice); // also tried alerting choice.textContent, choice.innerHTML and choice.firstChild
});
var choice = $('table#load-opt tr td');
 choice.on('click', function(){
 alert($(this).text());
});