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

时间:2015-01-15 18:51:35

标签: 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 = 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);
     });

我试图获得&#34; id&#34;来自分类单元格的值,但警报只返回undefined。

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

编辑:关于这个的引号实际上不在代码中,我输入这个问题时输入错误,抱歉

2 个答案:

答案 0 :(得分:2)

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

糟糕,引用this

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

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

答案 1 :(得分:1)

这样的事情? 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() {});

之间

通过添加类&#34;分类&#34;,您可以指定哪个单元格可点击以提醒其相应的ID