请参考我的小提琴。我有一个动态行表,每个行都包含一个变量ID id="monitor_'+rowID+'"
的复选框,其中rowID是一个全局变量,在函数addText()
内声明没有关键字var
。我试图使用.each(function(){});
通过行循环,但它似乎不工作。
我的目的是查看每行中的复选框是否被选中。如果是,将调用函数monitoring
。下面是函数:
function monitoring() {
$('#test').append("checked");
fbType = $('td.nth(2)', $(this)).text();
fbNum = $('td.nth(3)', $(this)).text();
$('#test').append(fbType + fbNum);
$.post('/request', {
inputText: fbNum,
key_pressed: fbType.toString()
}).done(function (reply) {
if (reply == "on") {
$('#test').append("on ");
} else {
$('#test').append("off ");
}
});
}
您可以添加行,但选择一个选项并添加一个数字,然后按"添加"按钮。第一个.append("checked")
是为了确保我调用函数,但奇怪的是,它只显示一次,即使我有几个复选框,我选中了他们。
另一个问题,我不能显示以下内容:
fbType = $('td.nth(2)', $(this)).html();
fbNum = $('td.nth(3)', $(this)).html();
为什么会这样?这是至关重要的,因为正如您所看到的,我将把这些数据发布到一个python函数…
请告诉我我的代码有什么问题,我应该怎么做。
您可以更改每个循环中的元素。我改变了第n选择器来获取值。在您的示例中,您尝试使用jQuery函数作为CSS选择器。
$('#monitor').click(function () {
$('#status_table tr [id^="monitor_"]:checked').each(function () {
monitoring($(this).parents('tr'));
});
});
function monitoring($row) {
$('#test').append("checked");
fbType = $row.find('td:nth-child(2)').html();
fbNum = $row.find('td:nth-child(3)').html();
$('#test').append(fbType + fbNum);
}
你这里有一个算法问题:
$('#monitor').click(function () {
$('#status_table tr').each(function () {
$check = $('#status_table tr #monitor_' + rowID)
if ($check.is(':checked')) {
monitoring();
}
});
});
您正在使用rowID,而正如您所说,它是一个全局变量,然后当您在最后一个rowID(如果有3行,rowID = 3)使用。each()。
你能试着解释一下你想用这些行做什么吗?
fbType = $('td.nth(2)', $(this)).html();
fbNum = $('td.nth(3)', $(this)).html();