AJAX JQUERY 相关:- 如何在 AJAX 加载的页面上调用 AJAX


AJAX JQUERY RELATED:- How to call ajax on a ajax loaded page

假设我通过AJAX调用搜索。现在我想在页面上调用另一个 AJAX,该 AJAX 将由 AJAX 加载并且可以编辑。

<input type="button" id=sb value="Search Contacts" />
<input type="text" name="search" placeholder="search contacts" id="s1" style="display:none" />
<input type="text" id="b1" value="<?php echo $username ; ?>" style="display:none;float:left" />

我正在使用 AJAX 调用 php 文件。现在,我进一步想对通过 AJAX 加载的数据调用 AJAX。可能吗?

我可以通过 AJAX

调用包含页面的 jquery (AJAX) 吗?这样行得通吗?

我正在通过 AJAX 创建搜索框及其从数据库中显示结果,它以表格的形式给我。现在,我想将该表转换为实时表,单击后可以对其进行编辑并存储数据。

我知道如何创建实时表,

但不知道如何同时调用搜索和实时表。

你需要使用 Jquery 委托将事件绑定到活动元素

$('body').delegate('#yourtableid','click /*or watever you want*/',function(){
//do stuff here (editing)
});

假设你基本上有类似这个结构和jQuery 1.7+的东西

<div id="resultContainer">
  <table>
    <tr>
      <!-- editable data -->
      <span>data</span>
      <!-- that turns into like this on click -->
      <form>
        <input type="text" value="data">
        <input type="submit" value="save">

在父页面上,您需要附加"委托处理程序",这些处理程序是放置在为当前或将来的子元素执行的父元素上的事件处理程序。

因此,假设我们有一个容器div id'ed "resultContainer",您可以在其中转储实时数据,我们附加它处理程序:

//attach a delegated submit handler on resultContainer for forms
$('#resultContainer').on('submit','form',function(event){
    //do what you want when an item is edited
    //"this" inside here is the DOM element form, which fired the submit
    //prevent the default submit action using event.preventDefault()
    //$(this).serialize() turns the form data into a serialized query string
});

我的应用程序的PHP页面在这个实时搜索中,我被应用了

<html>
<head>
<script type="text/javascript">
//====================================================================================================================
//============================================first call when page load=============================================
    function ajaxCall(n)
    {
        //alert("display ajacCall ");
        //alert("page no ::"+n);
        var ajaxRequest;
        try
        {
                ajaxRequest = new XMLHttpRequest();
        } 
        catch (e)
        {
                try
                {
                        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) 
                {
                        try
                        {
                                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                         } 
                    catch (e)
                    {
                            alert("Your browser broke!");
                            return false;
                    }
            }
        }
        ajaxRequest.onreadystatechange=function()
        {
                if(ajaxRequest.readyState==4)
                {
                        //alert("Response :: "+ajaxRequest.responseText);
                        document.getElementById("displayButton").innerHTML=ajaxRequest.responseText;
                        if(n!=0)
                        {
                            disp_Record(n);
                        }
                        else
                        {
                            disp_Record(0);
                        }
                }
        }
        var query="ajaxhendler.php?msg=select";
        //alert("Request ::" +query);
        ajaxRequest.open("POST",query,true);
        ajaxRequest.send();
    }
//================================================Display Recodr to button Press================================================
//================================================Display Record to Static Button===============================================
    function disp_Record(n)
    {   
        //alert("display record function");
        var ajaxRequest;
        try
        {
                ajaxRequest = new XMLHttpRequest();
        } 
        catch (e)
        {
                try
                {
                        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) 
                {
                        try
                        {
                                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                        } 
                    catch (e)
                    {
                            alert("Your browser broke!");
                            return false;
                    }
            }
        }
        ajaxRequest.onreadystatechange=function()
        {
                if(ajaxRequest.readyState==4)
                {
                        //alert("Response :: "+ajaxRequest.responseText);
                        document.getElementById("displayRecord").innerHTML=ajaxRequest.responseText;
                }
        }
        //alert("txt::"+ txt)
        var query="ajaxhendler.php?msg=selectRecordNo&no_of_record="+n;
        //alert("Request ::" +query);
        ajaxRequest.open("POST",query,true);
        ajaxRequest.send();
    }
//==============================================For Live search==============================================================
//===========================================For live search Display Button=================================================
    function callSearch(n,text)
    {   
        //alert("call Search First Function");
        var ajaxRequest;
        try
        {
                ajaxRequest = new XMLHttpRequest();
        } 
        catch (e)
        {
                try
                {
                        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) 
                {
                        try
                        {
                                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                        } 
                    catch (e)
                    {
                            alert("Your browser broke!");
                            return false;
                    }
            }
        }
        ajaxRequest.onreadystatechange=function()
        {
                if(ajaxRequest.readyState==4)
                {
                        //alert("Response :: "+ajaxRequest.responseText);
                        document.getElementById("displayButton").innerHTML=ajaxRequest.responseText;
                        if(n!=0)
                        {
                            disp_Record_live(n,text);
                        }
                        else
                        {
                            disp_Record_live(0,text);
                        }
                }
        }
        var txt=document.getElementById("txtsearch").value;
        //alert("txt::"+ txt)
        var query="ajaxhendler.php?msg=selectRecord&txt="+txt;
        //alert("Request ::" +query);
        ajaxRequest.open("POST",query,true);
        ajaxRequest.send();
    }
//====================================================================================================================
function disp_Record_live(n,text)
    {   
        //alert("Display Record live Second Function");
        var ajaxRequest;
        try
        {
                ajaxRequest = new XMLHttpRequest();
        } 
        catch (e)
        {
                try
                {
                        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) 
                {
                        try
                        {
                                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                        } 
                    catch (e)
                    {
                            alert("Your browser broke!");
                            return false;
                    }
            }
        }
        ajaxRequest.onreadystatechange=function()
        {
                if(ajaxRequest.readyState==4)
                {
                        //alert("Response :: "+ajaxRequest.responseText);
                        document.getElementById("displayRecord").innerHTML=ajaxRequest.responseText;
                }
        }
        var txt=document.getElementById("txtsearch").value;
        //alert("txt::"+ txt)
            var query="ajaxhendler.php?msg=select_Record_No_live&no_of_record_live="+n+"&findR="+txt;
        //alert("Request ::" +query);
        ajaxRequest.open("POST",query,true);
        ajaxRequest.send();
    }
</script>

</head>
<body onLoad="<?php if($_GET['txt']=="")
                    {
                        echo "ajaxCall(";
                        $n=0; if($_GET['page']!=null)
                            { 
                                echo $_GET['page']; 
                            }
                            else
                            { 
                                echo $n;
                            }
                        echo ")";
                    }
                    else
                    {
                        echo "callSearch(";
                        echo $_GET['page'].",'".$_GET['txt']."')";
                    } ?>" onUnload="msg()" bgcolor="#CCCCCC">
        <form>
 <table width="100%">

  <tr bgcolor="#CCFFCC">
                                <td  colspan="5"> 
                                       <font color="#CC6666">Search Record By Name</font><input type="text" name="txtsearch" id="txtsearch" value="<?php echo $_GET['txt'];?>" onKeyUp="callSearch(<?php
                                       $n=0; 
                                       if($_GET['page']!=null)
                                       { 
                                        echo $_GET['page'].",'".$_GET['txt']."'";
                                        }
                                        else
                                        { echo $n;}
                                        ?> )"/>
                                </td>
                                <td align="right">  
                                        <a href="insert.php">Insert Record</a>
                                </td>
                            </tr>
                </table>
                <table width="100%">
                             <tr>
                                    <td>
                                        <div id="displayRecord"></div>
                                    </td>
                            </tr>
                            <tr>
                                <td>
                                    <div id="displayButton">
                                    </div>
                                </td>
                            </tr>
                </table>
        </form>
</body>
</html>

这是我的处理程序页面.php

<?php
        $page_name = "http://localhost/desk.php";
        $msg=$_GET['msg'];
if($msg=='select')
        {
                load_pages($_GET['page']);
        }
        if($msg=="selectRecord")
        {
                live_search_pages();
        }
        if($msg=="selectRecordNo")
        {
            $n=$_GET['no_of_record'];
            disp_Record($n);
        }
        if($msg=="select_Record_No_live")
        {
            $n=$_GET['no_of_record_live'];
            live_search($n,$t);
        }
//==================================================================pages============================================
//=====================================================Static button================================================
function load_pages($page)
{
        $cn=mysql_connect("localhost","root","");
        mysql_select_db("per_info",$cn);
        $query="SELECT count(*) 'TR' from p_info";
        $rs = mysql_query($query);
        $rec = mysql_fetch_array($rs);
        $n=$rec['TR'];
        if($n>0)
        {
        $pages=ceil($n/3);
        $i=0;
        echo "<table align='"center'"><tr><td align='"right'" bgcolor='"#666666'"><font color='"#FFFFFF'">Pages : </td><td align='"center'" bgcolor='"#666666'">";
        while($i<$pages)
        {
                echo "<input type='"button'" value='"".($i+1).""; echo "'" onclick='"disp_Record(".($i*3).")'" />";
                $i=$i+1;
        }
        echo "</td></tr></table>";
        }
        else
        {
            echo "No Record Found";
        }
}
//============================================Selected record Display By Button========================================
//==========================================Display Record for static Record==========================================
        function disp_Record($btn)
        {
                $cn=mysql_connect("localhost","root","");
                mysql_select_db("per_info",$cn);
                //$btn=1;
                    $query="SELECT u.p_id 'ID', u.p_name 'NAME', u.p_add 'ADDRESS', l1.loc_name 'CITY', l2.loc_name
                        'STATE',u.p_gen
                        'GENDER', u.p_photo 'PIC', u.p_phone 'PH'
                        FROM p_info u, tbl_state l1, tbl_state l2
                        WHERE u.p_city = l1.loc_id
                        AND l2.loc_id = l1.loc_parent ORDER BY u.p_id desc LIMIT ".$btn.", 3";
                    $rs = mysql_query($query);
                    echo "<table width='"100%'" frame='"hsides'" >
                            <th bgcolor='"#666666'"><font color='"#FFFFFF'">Name</font></th>
                                <th bgcolor='"#666666'"><font color='"#FFFFFF'">Address</font></th>
                                <th bgcolor='"#666666'"><font color='"#FFFFFF'">City</font></th>
                                <th bgcolor='"#666666'"><font color='"#FFFFFF'">State</font></th>
                                <th bgcolor='"#666666'"><font color='"#FFFFFF'">Gender</font></th>
                                <th bgcolor='"#666666'"><font color='"#FFFFFF'">Photo</font></th>
                                <th bgcolor='"#666666'"><font color='"#FFFFFF'">Phone No</font></th>
                                <th bgcolor='"#666666'"></th>
                                <th bgcolor='"#666666'"></th>";
                while($row=mysql_fetch_array($rs))
                {
                    echo "<tr>
                                <td>".$row['NAME']."</td>
                                <td>".$row['ADDRESS']."</td>
                                <td>".$row['CITY']."</td>
                                <td>".$row['STATE']."</td>
                                <td>";if($row['GENDER']=="1")
                                        {
                                                echo "Male";
                                        }
                                        else
                                        {
                                                echo "Female";
                                        }
                                echo "</td>
                                <td>
                                    <img src='"".$row['PIC']."'" alt='"Pulpit rock'" width='"50'" height='"50'" />      
                                </td>";
                                echo "<td>".$row['PH']."</td>
                                <td><input type='"button'" value='"Edit'" onclick='"getid(".$row['ID'].",".$btn.",'')'"/></td>
                                <td><input type='"button'" value='"Delete'" onclick='"callDelete(".$row['ID'].",".$btn.",'')'"/></td>
                          </tr>";
                }

        }
//==========================================================Live search By Button=====================================
function live_search_pages()
{
        $txt=$_GET['txt'];
        $cn=mysql_connect("localhost","root","");
        mysql_select_db("per_info",$cn);
        $query="SELECT count(*) 'TR' from p_info where p_name like '".$txt."%'";
        $rs = mysql_query($query);
        $rec = mysql_fetch_array($rs);
        $n=$rec['TR'];
        if($n>0)
        {
            $pages=ceil($n/3);
            $i=0;
            echo "<table align='"center'"><tr><td align='"right'" bgcolor='"#666666'"><font color='"#FFFFFF'">Pages : </td><td align='"center'" bgcolor='"#666666'">";
            while($i<$pages)
            {
                    echo "<input type='"button'" value='"".($i+1).""; echo "'" onclick='"disp_Record_live(".($i*3).")'" />";
                    $i=$i+1;
            }
            echo "<td></tr></table>";
            }
        else
        {
            echo "No record Found";
        }
}
//================================================================Live search==========================================
        function live_search($btn,$text)
        {
                $txt=$_GET['findR'];
                $text=$_GET['text'];
                $cn=mysql_connect("localhost","root","");
                mysql_select_db("per_info",$cn);

                if($text==null)
                {
                $query="SELECT u.p_id 'ID', u.p_name 'NAME', u.p_add 'ADDRESS', l1.loc_name 'CITY', l2.loc_name 'STATE', u.p_gen 
                        'GENDER', u.p_photo 'PIC', u.p_phone 'PH'
                        FROM p_info u, tbl_state l1, tbl_state l2
                        WHERE u.p_city = l1.loc_id
                        AND l2.loc_id = l1.loc_parent
                        AND u.p_name LIKE '".$txt."%' ORDER BY u.p_id desc LIMIT ".$btn.", 3";
                }
                else
                {
                    $query="SELECT u.p_id 'ID', u.p_name 'NAME', u.p_add 'ADDRESS', l1.loc_name 'CITY', l2.loc_name 'STATE',u.p_gen
                        'GENDER', u.p_photo 'PIC', u.p_phone 'PH'
                        FROM p_info u, tbl_state l1, tbl_state l2
                        WHERE u.p_city = l1.loc_id
                        AND l2.loc_id = l1.loc_parent
                        AND u.p_name LIKE '".$text."%' ORDER BY u.p_id desc LIMIT ".$btn.", 3";
                }
                if($rs=mysql_query($query))
                {
                    echo "<table width='"100%'" frame='"hsides'">
                                <th bgcolor='"#666666'"><font color='"#FFFFFF'">Name</font></th>
                                    <th bgcolor='"#666666'"><font color='"#FFFFFF'">Address</font></th>
                                    <th bgcolor='"#666666'"><font color='"#FFFFFF'">City</font></th>
                                    <th bgcolor='"#666666'"><font color='"#FFFFFF'">State</font></th>
                                    <th bgcolor='"#666666'"><font color='"#FFFFFF'">Gender</font></th>
                                    <th bgcolor='"#666666'"><font color='"#FFFFFF'">Photo</font></th>
                                    <th bgcolor='"#666666'"><font color='"#FFFFFF'">Phone No</font></th>
                                    <th bgcolor='"#666666'"></th>
                                    <th bgcolor='"#666666'"></th>";
                    while($row=mysql_fetch_array($rs))
                    {
                            echo "<tr>
                                        <td>".$row['NAME']."</td>
                                        <td>".$row['ADDRESS']."</td>
                                        <td>".$row['CITY']."</td>
                                        <td>".$row['STATE']."</td>
                                        <td>";if($row['GENDER']=="1")
                                                {
                                                        echo "Male";
                                                }
                                                else
                                                {
                                                        echo "Female";
                                                }
                                        echo "</td>
                                        <td>
                                            <img src='"".$row['PIC']."'" alt='"Pulpit rock'" width='"50'" height='"50'" />      
                                        </td>";
                                        echo "<td>".$row['PH']."</td>
                                        <td><input type='"button'" value='"Edit'" onclick='"getid(".$row['ID'].",".$btn.",'".$txt."')'"/></td>
                                        <td><input type='"button'" value='"Delete'" onclick='"callDelete(".$row['ID'].",".$btn.",'".$txt."')'"/></td>
                                  </tr>";

                    }
                }
                else
                {
                    echo "error";
                }
        }
?>

我希望它能帮助你