Php 访问<;td>;点击文本

Php 访问<;td>;点击文本,php,jquery,html,onclick,html-table,Php,Jquery,Html,Onclick,Html Table,我想获取单击哪个表数据的文本。i、 e.如果单击post_no,则获取$post_no值;如果单击description,则获取单词'description'。我尝试了很多方法,但都没有得到我想要的。方法是什么 我的php代码: echo "<table border=1>"; echo "<tr><th>Post No</th><th>Writer ID</th><th>Writer Name&

我想获取单击哪个表数据的文本。i、 e.如果单击post_no,则获取$post_no值;如果单击description,则获取单词'description'。我尝试了很多方法,但都没有得到我想要的。方法是什么

我的php代码:

echo "<table border=1>";
        echo "<tr><th>Post No</th><th>Writer ID</th><th>Writer Name</th><th>Title</th><th>Description</th><th>Summary</th><th>Approval</th></tr>";
        while ($row=  mysqli_fetch_array($r2)){
            $post_no=$row['post_no'];
            $writer_id=$row['writer_id'];
            $writer_name=$row['writer_name'];
            $title=$row['title'];
            $description=$row['description'];
            $summary=$row['summary'];


            echo "<tr height='50' class='x"."". " '>";
            echo "<td class='r'>".$post_no."</td>";
            echo "<td class='r'>".$writer_id."</td>";
            echo "<td class='r'>".$writer_name."</td>";
            echo "<td class='r'>".$title."</td>";
            echo "<td class='r'>".'description'."</td>";
            echo "<td class='r'>".'summary'."</td>";
            echo "<td class='r'>Approve</td>";
            echo "</tr>";
        }
        echo "</table>";
echo”“;
echo“Post NoWriter IDWriter NameTitleDescription Summary Approval”;
而($row=mysqli\u fetch\u数组($r2)){
$post_no=$row['post_no'];
$writer_id=$row['writer_id'];
$writer_name=$row['writer_name'];
$title=$row['title'];
$description=$row['description'];
$summary=$row['summary'];
回声“;
回显“$post_no.”;
回显“$writer_id.”;
回显“$writer_name.”;
回声“$title.”;
回显“'description.”;
回显“'summary.”;
回应“批准”;
回声“;
}
回声“;
javascript代码

<script>
        $(document).ready(function(){
            $(".x").each(function(i){
                $(this).click(function(){
                    console.log($(this).children().eq(i).text());
                });
            });
        });
    </script>

$(文档).ready(函数(){
$(“.x”)。每个(功能(i){
$(此)。单击(函数(){
log($(this.children().eq(i.text());
});
});
});

那么如果单击相应的div,您只需要
$post\u no
的值?那你为什么不把事件绑定到那个类上呢

$(".r").click(function() {
    console.log($(this).text()) 
});
不管出于什么原因,人们都会继续否决这项议案,给你,菲德尔。如果我做错了什么,告诉我,不要只是投反对票

我会这么做的

$('.x').click(function (e) {
    var $target = $(e.target);
    console.log($target.text());
});

那么您想知道您单击了什么值,但绑定仍保留在行上? 完全可能:

$(document).ready(function(){
        $(".x").click(function(event){
            console.log(event.target); //Log where you clicked
            console.log($(event.target).text());
        });
    });
为什么要这样做?
在单击类为x(每行)的元素时添加到单击事件的事件处理程序中,我们传递对事件本身的引用。
在本参考资料中,我们可以访问有关事件的大量信息,如本例中的目标。目标是实际单击的元素

因为Javascript可以处理事件冒泡,所以您不需要在每个元素上设置处理程序,但可以在顶层设置它(即使在“body”上也可以),通过这个(event.target),您可以看到用户真正单击的位置

因为我们现在知道了用户单击的元素,所以可以将该引用传递给jQuery对象($(event.target))并使用text()函数。

$(函数(){
$(“#table_test”).find(“td”).each(function(){
$(此)。单击(函数(){
警报($(this.text());
});
});
});

一个
两个
三
四

不知道为什么每个人都在寻求复杂的解决方案。只需使用针对所需元素的选择器:

$("tr.x td.r").click(function(){
    console.log($(this).text());
});
甚至只是:

$(".r").click(function(){
    console.log($(this).text());
});
动态要素: 如果您想要更高效的东西,可以使用委托事件处理程序。这样做的优点是,如果将项动态添加到表中,它仍然可以工作:

$("table").on('click', 'td.r', function(){
    console.log($(this).text());
});
这是通过侦听事件(在本例中为
单击
)冒泡到未更改的祖先元素来实现的,然后它将jQuery过滤器(在本例中为
td.r
)仅应用于冒泡链中的元素。然后,它将函数仅应用于导致事件的元素。所有这些的结果是,元素只需要在事件发生时存在,而不需要在事件注册时存在

委派事件的目标应该是一个不变的祖先元素。在本例中,我只选择了
。如果没有关闭/方便的内容,则默认使用
文档
(不要使用
正文
,因为它有一个可以停止鼠标事件响应的错误)

echo”“;
echo“Post NoWriter IDWriter NameTitleDescription Summary Approval”;
而($row=mysqli\u fetch\u数组($r2)){
$post_no=$row['post_no'];
$writer_id=$row['writer_id'];
$writer_name=$row['writer_name'];
$title=$row['title'];
$description=$row['description'];
$summary=$row['summary'];
回声“;
回显“$post_no.”;
回显“$writer_id.”;
回显“$writer_name.”;
回声“$title.”;
回显“$description.”;
回显“$summary.”;
回应“批准”;
回声“;
}
回声“;
//jquery部分

<script>
        $(document).ready(function(){
            $(document).on('click','.r',function(event){
               event.preventDefault();
               var td_txt = $(this).text();
              console.log(td_txt);
            //or you can use alert.
           });
        });
    </script>

$(文档).ready(函数(){
$(文档).on('单击','.r',函数(事件){
event.preventDefault();
var td_txt=$(this).text();
console.log(td_-txt);
//或者您可以使用警报。
});
});
“description”和“summary”的php值没有正确连接。
在jquery部分,您也可以使用alert来获取相应的
td

值。单击该列或行时,您需要文本吗?为什么不直接指向
class=“r”
而不解决您的问题,但是你错过了一些
$
靠近
'description.
'summary.
。最好是看到最终的HTML代码(你在浏览器中看到的)而不是PHP代码。如果你选择一个答案,并停止明显地选择所有答案,你可能会避免进一步的向下投票。看着绿色的蜱虫在页面上弹来弹去,我感到头晕jQuery对象上很少需要
每个
来添加事件处理!只是复制了操作代码,并添加了使其工作的行。这不是传播不必要代码的借口:)没错,所以我删除了它:)我怎么才能在单击时获得文本,所以,无论是谁对此进行否决-请注意解释?@YUNOWORK strategic downvows,我在这个论坛上更多地看到了这种行为,你实际上可以找到关于如何进行战略否决的帖子(不一定在这个论坛上),我认为这是愚蠢的,不符合
<script>
        $(document).ready(function(){
            $(document).on('click','.r',function(event){
               event.preventDefault();
               var td_txt = $(this).text();
              console.log(td_txt);
            //or you can use alert.
           });
        });
    </script>