使用Javascript在Ajax响应中查找元素

使用Javascript在Ajax响应中查找元素,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个包含一些输入、选择和其他内容的表单,我编写了一个搜索栏,它将发送一个Ajax请求,以获取结果,从而找到要附加到表单的联系人: index.php-表单 index.php-Ajax 文件contact.search.php正在处理请求,并将回显包含以下元素的表: 表: 结果TR: contact.search.php 然后,用户必须单击其中一个TR来选择所需的联系人,我需要将此选择显示到其他输入中,这些输入将用于保存表单。但当我试图用这个脚本捕获这些数据时: Javascript 控制台

我有一个包含一些输入、选择和其他内容的表单,我编写了一个搜索栏,它将发送一个Ajax请求,以获取结果,从而找到要附加到表单的联系人:

index.php-表单

index.php-Ajax

文件contact.search.php正在处理请求,并将回显包含以下元素的表: 表: 结果TR:

contact.search.php

然后,用户必须单击其中一个TR来选择所需的联系人,我需要将此选择显示到其他输入中,这些输入将用于保存表单。但当我试图用这个脚本捕获这些数据时:

Javascript

控制台显示[对象对象]/[对象对象]/[对象对象]/[对象对象对象]/[对象对象]/

我试着遵循以下提示:

但它只在数据位于初始页面时才起作用,而在Ajax查询加载内容时不起作用


有人能帮我让它工作吗?我想通过点击tr从Ajax结果中显示其每个td的数据到控制台中。感谢您的帮助。

除非您使用此选项,否则您只是在引用所有指定的类

 $(document).on("click","#contactResult tr",function(){ 
        var nom=$(this).find( "td.item-nom" ).html();
        var prenom=$(this).find( "td.item-prenom" ).html();
        var tel=$(this).find( "td.item-tel" ).html();
        v.ar division=$(this).find( "td.item-division" ).html();
        var direction=$(this).find( "td.item-direction" ).html();
        console.log(nom + " / " + prenom + " / " + tel + " / " + division + " / " + direction + " / ");
        });

除非您使用它,否则您只是引用所有指定的类

 $(document).on("click","#contactResult tr",function(){ 
        var nom=$(this).find( "td.item-nom" ).html();
        var prenom=$(this).find( "td.item-prenom" ).html();
        var tel=$(this).find( "td.item-tel" ).html();
        v.ar division=$(this).find( "td.item-division" ).html();
        var direction=$(this).find( "td.item-direction" ).html();
        console.log(nom + " / " + prenom + " / " + tel + " / " + division + " / " + direction + " / ");
        });

这很简单。。。它在处理这件事,我真傻,不去想它。非常感谢你的帮助:这很简单。。。它在处理这件事,我真傻,不去想它。非常感谢您的帮助:
while ($ligne = sqlsrv_fetch_object($query)) {
    $body .= "
            <tr class='select'>
                <td class='item-nom'>".$ligne->nom."</td>
                <td class='item-prenom'>".$ligne->prenom."</td>
                <td class='item-tel'>".$ligne->tel."</td>
                <td class='item-division'>".$ligne->division."</td>
                <td class='item-direction'>".$ligne->direction."</td>
            </tr>";
}
<script type="text/javascript">
    $("#contactResult tr").click(function(){ 
        var nom=$( "td.item-nom" );
        var prenom=$( "td.item-prenom" );
        var tel=$( "td.item-tel" );
        var division=$( "td.item-division" );
        var direction=$( "td.item-direction" );
        console.log(nom + " / " + prenom + " / " + tel + " / " + division + " / " + direction + " / ");
        });
</script>
 $(document).on("click","#contactResult tr",function(){ 
        var nom=$(this).find( "td.item-nom" ).html();
        var prenom=$(this).find( "td.item-prenom" ).html();
        var tel=$(this).find( "td.item-tel" ).html();
        v.ar division=$(this).find( "td.item-division" ).html();
        var direction=$(this).find( "td.item-direction" ).html();
        console.log(nom + " / " + prenom + " / " + tel + " / " + division + " / " + direction + " / ");
        });