jQuery的返回链接不工作时,阿贾克斯 [英] jQuery Return links is not working when Ajax

查看:97
本文介绍了jQuery的返回链接不工作时,阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery的返回链接不工作。我已经使用jQuery和基本的Ajax特性。我的jQuery返回从文件的链接 Links_ajax.php

jQuery return links are not working. I have Used jQuery and the basic Ajax feature. My jQuery returns the links from file Links_ajax.php.

我给code样本。

GetCustomerData.php有:

GetCustomerData.php has:

<html>
    <script type="text/javascript">
    <script src="ajax.js" type="text/javascript">

        $(function() {
            .....

            $.ajax({
                type: 'POST',
                url: 'Links_ajax.php',
                data: 'category='+category  ,
                success: function(data){
                    $("#response").html(data);
                }
            });
        }
        ......
    }

    ...

    <! Links return here
        <div id="response">
        </div>
</html>

<?
  echo "Some Code";


?>

  ....

我Links_ajax.php有以下code

My Links_ajax.php has the following code

....

echo '<a href="getCustomerData.php?id=10" onclick="requestCustomerInfo();return false;">show me </a>';
echo '<a href="getCustomerData.php?id=11" onclick="requestCustomerInfo();return false;">show me </a>';

....

现在我来给主页getCustomerData.php。我已经使用了Ajax特性,返回从Links_ajax.php链接。

Now I come to the Main Page getCustomerData.php. I have used the Ajax feature that returns link from Links_ajax.php.

 .....

所以,我ajax.js有以下阿贾克斯脚本。

So, my ajax.js has the following Ajax scripts.

var url = "GetCustomerData.php?id="; // The server-side script
function handleHttpResponse() {
    if (http.readyState == 4) {
        if (http.status == 200) {
            var results = http.responseText;
            document.getElementById('divCustomerInfo').innerHTML = results;
        }
    }
}

function requestCustomerInfo(event) {
    if(event &&event.preventDefault) event.preventDefault();
    else if (window.event && window.event.returnValue)
        window.eventReturnValue = false;
        var sId = 10;
        http.open("GET", url + escape(sId), true);
        http.onreadystatechange = handleHttpResponse;
        http.send(null);
    }

    function getHTTPObject() {
        var xmlhttp;

        if (window.XMLHttpRequest){
            xmlhttp = new XMLHttpRequest();
        }
        else if (window.ActiveXObject){
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            if (!xmlhttp){
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
        }
        return xmlhttp;
    }

    var http = getHTTPObject(); // We create the HTTP object

......

......

现在我来到这个问题。当我执行GetCustomerdata.php,我能够得到ajax.JQuery的所有环节。 而在点击了,我需要一个基本的Ajax特性,因为加载同一个&LT; D​​IV&GT; &LT; / DIV&GT; 。但我无法看到它,即使我做了。 有什么错我的code?还是我必须使用其他功能?

Now I come to the problem. When I execute the GetCustomerdata.php, I am able to get all the links from ajax.JQuery. And upon clicking that, I need a basic Ajax feature since loading the same <div> </div>. But I am unable to view it even though I done. Is there anything wrong in my code? Or do I have to use any other feature?

推荐答案

首先,我不知道为什么你写了一个自定义函数HTT prequest,而你已经有jQuery对象,事实上你已经使用$阿贾克斯上方,但随后创建getHTTPObject()之后。

First i wonder why you write a custom function HTTPrequest while you already have jquery object, infact you already use $.ajax at the top but then create getHTTPObject() later.

现在的问题是JavaScript函数requestCustomerInfo()从来没有委托来处理在Ajax响应数据的链接,你需要的功能委托作为Ajax请求的回调

The problem is the javascript function requestCustomerInfo() never delegate to handle the link at the ajax response data, you need to delegate the function as a callback of the ajax request

$.ajax({
       type:'POST',
       url: 'Links_ajax.php',
       dataType: 'html',
       data:'category='+category,
       success: function(data){
           $("#response").html(data);
    	   // here add the link click handler
    	   $("#response a").click(function(){
    			// make http get request & custom handler
    			$.get('GetCustomerData.php?id=10',function(result){
    				$('#divCustomerInfo').html(result);
    			}); return false;
    		});
       }
});

这篇关于jQuery的返回链接不工作时,阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆