Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.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
JQuery从动态表获取文本:第n个选择器_Jquery_Json_Jquery Selectors - Fatal编程技术网

JQuery从动态表获取文本:第n个选择器

JQuery从动态表获取文本:第n个选择器,jquery,json,jquery-selectors,Jquery,Json,Jquery Selectors,我有一个我无法解决的问题,我已经找到了一些帮助,但不是我需要的帮助 我需要访问数据[I]。单击的行中没有变量。我只是不能让我的代码在底部显示这个 下面是从JSON查询创建动态表的代码: html = '<table class="tbl small">' $time = 0 for (var i = 0; i< len; i++) { $time = (parseInt($time)+parseInt(data[i].time)); if (

我有一个我无法解决的问题,我已经找到了一些帮助,但不是我需要的帮助

我需要访问
数据[I]。单击的行中没有变量。我只是不能让我的代码在底部显示这个

下面是从JSON查询创建动态表的代码:

html = '<table class="tbl small">'
$time = 0
for (var i = 0; i< len; i++) {          
    $time = (parseInt($time)+parseInt(data[i].time));
    if (data[i].status == "MISSING") {
    $('#tottime').html('');
    } else {
        $('#tottime').html('Total time outstanding for ' + monthNames[d.getMonth()] + ' : ' + $time);
    }
    if (data[i].overdue == "Yes") {
        html = html + '<tr style="background: #FF0000">'
    } else if (data[i].status == "RE ASSES") {
        html = html + '<tr style="background: #D0C0C0">'
    } else if (data[i].status == "REPAIR") {
        html = html + '<tr style="font-style: italic">'
    } else{
        html = html + '<tr>'
    }
    html = html + '<td class="border col1">'+data[i].line+'</td>'
    html = html + '<td class="border col2">'+data[i].no+'</td>'
    html = html + '<td class="border col3">'+data[i].desc+'</td>'           
    html = html + '<td class="border col4">'+data[i].loc+'</td>'            
    html = html + '<td class="border col5">'+data[i].time+'</td>'           
    html = html + '<td class="border col6">'+data[i].lastcal+'</td>'            
    html = html + '<td class="border col7">'+data[i].frequency+'</td>'          
    html = html + '<td class="border col8">'+data[i].status+'</td>' 
    if (data[i].external == "E" ) {
        html = html + '<td class="border col9">&#10004;</td>'   
    } else {
        html = html + '<td class="border col9"></td>'
    }
    html = html + '</tr>'           
    }
html = html + '</table>'
$('#output').html(html);

在单击事件
中,此
是已单击的
td
,如果要搜索第二个
td
,则应“向上”移动到第一个
tr

$('#output').on('click','td',function() {

    // $(this) is the clicked td
    // .closest("tr") returns the first tr parent ( in this case the same as .parent() )
    // .find(':nth-child(2)') gives you the second child of the tr
    var html = $(this).closest('tr').find(':nth-child(2)').text()

    $('#msgBox').html(html);
    $('#msgBox').dialog('option','title','Calibration Complete')
    $('#msgBox').dialog('open')
})

另外(在一个完全不同的领域),尝试给你的a变量一个作用域,而不是使用全局变量。这可以为您节省很多麻烦,您只需在第一次使用之前添加
var
(如上所述)。

您得到的结果或错误是什么?您的
#输出
只有一个子项:您的
。非常感谢,这增加了JQuery的学习过程。工作完美,感谢您无价的帮助!或者,您也可以使用var html=$(this).parent().find(':nth child(2)').text()@Taff,是的,正如我在行上方的注释中所说,在本例中,
closest(“tr”)
parent()
是相同的。我个人更喜欢最近的
,因为:#1选择器澄清结果是什么#2如果不是表,则减少代码对标记的依赖性
$('#output').on('click','td',function() {

    // $(this) is the clicked td
    // .closest("tr") returns the first tr parent ( in this case the same as .parent() )
    // .find(':nth-child(2)') gives you the second child of the tr
    var html = $(this).closest('tr').find(':nth-child(2)').text()

    $('#msgBox').html(html);
    $('#msgBox').dialog('option','title','Calibration Complete')
    $('#msgBox').dialog('open')
})