2012-06-18 25 views
0

我想要做一个简单的AJAX函数从查询字符串发送数据,并追加DIV。 。我尝试了各种不同的方法,但没有更新div。如果它更新,它将返回NULL,否则它会转到request.php页面。如果我在click事件下移动预设的默认功能,它什么也不做。 任何帮助将不胜感激! TIA在ajax函数中使用查询字符串

<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script> 
<script type="text/javascript"> 
$(function() 
{ 
    $("#link").click(function(e) 
    { 

     $.ajax(
       { 
        type : "GET", 
        url  : "request.php", 
        data : { id: link.attr('id') }, 
        success : function(response) 
        { 
         $("#ajaxresponse div").fadeOut("fast", function() 
         { 
          $("#ajaxresponse div").remove(); 
          $("#ajaxresponse").append($(response).hide().fadeIn()); 
         }); 

        } 
       }); 

     e.preventDefault(); 
    }); 
}); 

</script> 
</head><body> 
<h1> 
    AJAX with PHP 
</h1> 
<div class="content"> 

<a href="request.php?id=1" id="link" data-id="1" >Submit Link</a> 

</div> 
<div id="ajaxresponse"> 
    <div>please submit the form</div> 
</div> 

的request.php如下:

<?php 
$username = $_GET['id']; 


echo getTemplate($username); 

function getTemplate($username) 
{ 
return '<div class="box"> 
    <h1>The ID is</h1> 
    <div class="meta">username: '.$username.'</div> 
</div>'; 

} 

?> 
+0

什么数据,如果你直接访问的URL,并在URL的到底是什么在'response' –

+0

我只是想发送“1”:request.php ID = 1 – Jjames

回答

1

我猜你是想读值的链路ID被点击了错误的方式。这应该工作。

$(function() 
{ 
    $("#link").click(function(e) 
    { 
     var link=$(this); 
     e.preventDefault(); 
     $.ajax(
       { 
        type : "GET", 
        url  : "request.php", 
        data : { id: link.attr('id') }, 
        success : function(response) 
        { 
         $("#ajaxresponse div").fadeOut("fast", function() 
         { 
          $("#ajaxresponse div").html(response).fadeIn(); 
         }); 

        } 
       }); 

    }); 
}); 

你甚至可以使用get方法是jQuery的Ajax调用的短版与方法类型为GET

$(function() 
{ 
    $("#link").click(function(e) 
    { 
     var link=$(this); 
     e.preventDefault(); 
     $.get("request.php?id="+link.attr('id'),function(response){ 
       $("#ajaxresponse div").fadeOut("fast", function() 
       { 
        $("#ajaxresponse div").html(response).fadeIn(); 
       }); 
     });    
    }); 
}); 
+0

我只是试图发送“1”在URL的末尾:request.php?id = 1 ...有更好的方法,我只是新的:) – Jjames

+0

@Jjames:这没有问题。 – Shyju

+0

当我运行它,我回到“链接”,锚标记的ID,而不是查询字符串上的1 – Jjames