Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 在ajax函数中使用查询字符串_Jquery - Fatal编程技术网

Jquery 在ajax函数中使用查询字符串

Jquery 在ajax函数中使用查询字符串,jquery,Jquery,我正在尝试使用一个简单的AJAX函数从查询字符串和追加DIV发送数据。我尝试了各种不同的方法,但没有一种方法会更新DIV。如果它更新,它将返回NULL,否则它将转到request.php页面。如果我在click事件下移动prevent default函数,它将不起任何作用。 任何帮助都将不胜感激!!短暂性脑缺血发作 <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script> <scrip

我正在尝试使用一个简单的AJAX函数从查询字符串和追加DIV发送数据。我尝试了各种不同的方法,但没有一种方法会更新DIV。如果它更新,它将返回NULL,否则它将转到request.php页面。如果我在click事件下移动prevent default函数,它将不起任何作用。 任何帮助都将不胜感激!!短暂性脑缺血发作

<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>

$(函数()
{
$(“#链接”)。单击(功能(e)
{
$.ajax(
{
键入:“获取”,
url:“request.php”,
数据:{id:link.attr('id')},
成功:功能(响应)
{
$(“#ajaxresponse div”).fadeOut(“快速”,函数()
{
$(“#ajaxresponse div”).remove();
$(“#ajaxresponse”).append($(response.hide().fadeIn());
});
}
});
e、 预防默认值();
});
});
AJAX与PHP
请提交表格

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>';

}

?>

我猜您试图以错误的方式读取正在单击的链接的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
方法,这是jqueryajax调用的一个简短版本,方法类型为
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();
               });   
       });             
    });
});

如果你直接访问url,那么数据是什么?响应中是什么?我只是想在url:request.php的末尾发送“1”?id=1我只是想在url:request.php?id=1的末尾发送“1”…可能有更好的方法,我只是新的:)@Jjames:这没有问题。当我运行它时,我会返回“link”,锚定标记的ID,而不是查询字符串上的1我得到了它…只是必须将link.attr更改为link.data…可能是我的蹩脚问题措辞:)…非常感谢!!!