单击单元格时,如何从表中获取共同响应的JavaScript值?
单击单元格时,我试图从此表中获取一个值:单击单元格时,如何从表中获取共同响应的JavaScript值?,javascript,php,jquery,html,Javascript,Php,Jquery,Html,单击单元格时,我试图从此表中获取一个值: while ($result = $query->fetch(PDO::FETCH_ASSOC)){ echo "<tr><td contenteditable = true column = firstName id = ".$result['id'].">".$result['firstName']."</td>"; echo "<td contenteditable =
while ($result = $query->fetch(PDO::FETCH_ASSOC)){
echo "<tr><td contenteditable = true column = firstName id = ".$result['id'].">".$result['firstName']."</td>";
echo "<td contenteditable = true column = lastName id = ".$result['id'].">".$result['lastName']."</td>";
echo "<td contenteditable = true column = day id = ".$result['id'].">".$result['day']."</td>";
echo "<td contenteditable = true column = dutiesPerformed id = ".$result['id'].">".$result['dutiesPerformed']."</td>";
echo "<td column = classification id = ".$result['id'].">".$result['classification']."</td>";
echo "</tr>\n";
}
我试图从分类单元格中获取“id”值,但警报只返回未定义的值
任何帮助都将不胜感激。谢谢
编辑:代码中并没有这段文字上的引号,我在输入这段文字时打错了,对不起
var overlayTeacherId = $('this').attr("id");
哎呀,看起来像是引用了这个
var overlayTeacherId = $(this).attr("id");
'this'
作为字符串选择标记名为
的元素<代码>此
,未加引号,用作对元素的引用。类似的内容
第1单元
第2单元
第三单元
第4单元
$(文档).ready(函数(){
$('.classification')。在('click',function()上{
var id=$(this.attr('id');
警报(id);
});
});
注意:问题可能出在.click()
与.on('click',function(){})之间代码>
通过添加一个类“classification”,您可以指定哪个单元格可以单击以提醒其各自的id错误标记(缺少引号、空格)加上引号中的“this”导致了问题。是的,代码中实际上不是这样的,我在键入此问题时发现了它,很抱歉,这确实提醒了正确的id,但问题是,然后它接受行中任何位置的单击(至少看起来是这样)。我需要它,这样它只有在用户点击分类单元时才会激活
var overlayTeacherId = $(this).attr("id");
<table>
<tr>
<td id="1">Cell 1</td>
<td id="2">Cell 2</td>
</tr>
<tr>
<td id="3" class="classification">Cell 3</td>
<td id="4">Cell 4</td>
</tr>
</table>
$(document).ready(function() {
$('.classification').on('click', function() {
var id = $(this).attr('id');
alert(id);
});
});