如何在单击单元格时从表中获取共同响应的 JavaScript 值


How to get co-responding JavaScript value from a table when a cell is clicked?

我试图在单击单元格时从此表中获取值:

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";
    }

这是我到目前为止获取值的内容(它不起作用):

$("td[column = classification]").click(function() { 
                $.post('dropDowntest.php', { id: this.id}, function(data) {

                })
                    var overlayTeacherId = $(this).attr("id");
                    alert(overlayTeacherId);
     });

我正在尝试从分类单元格中获取"id"值,但警报仅返回未定义。

任何帮助将不胜感激。谢谢!

编辑:这上面的引号实际上在代码中不存在,我在输入此查询时输入错误,抱歉

var overlayTeacherId = $('this').attr("id");

哎呀,看起来this被引用了。

var overlayTeacherId = $(this).attr("id");

'this'作为字符串选择标签名为 <this> 的元素。 this ,不加引号,充当对元素的引用。

像这样吗?http://jsfiddle.net/swm53ran/99/

<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);
    });
});

注意:也许问题可能出在.click().on('click', function() {});

通过添加类"分类",您可以指定可单击哪个单元格以提醒其各自的ID