jQuery Ajax:返回值给调用者?

2022-01-19 00:00:00 return return-value jquery ajax

我有一些 jQuery 代码.我调用了一个 Ajax 函数文件 file.php,其中包含一些字段,例如:

I have some jQuery code. I have called an Ajax function file, file.php, that has some fields, like:

<input type="radio" value="plz">Milk</input>.

我会再次分配给 jQuery 函数吗?如果是这样,我该怎么做?我附上了一个示例文件:

Will I assign into again jQuery function? If so, how do I do it? I attached a sample file:

<html>
    <head>
        <LINK REL=StyleSheet HREF="examples.css" TITLE="Contemporary" TYPE="text/css">
        <script src="jquery-1.2.6.js" type="text/javascript"></script>
        <script src="jquery-impromptu.1.6.js" type="text/javascript"></script>
        <script>
            $(document).ready(function(){
                $.ajax({
                    type:"GET",
                    url:"file.php",
                    data:id,
                    success:function(){
                        var txt=id;
                        $.prompt( txt,{ opacity: 0.2 });
                    },
                    error:function(){
                        window.location("ERRoR");
                    }
                });
            });
        </script>

    <body>
    </body>
</html>

推荐答案

success 函数带有一个参数,该参数包含获取的数据.所以在你的例子中:

The success function takes a parameter, which contains the fetched data. So in your example:

$(document).ready(
    function(){ $.ajax({
       type:"GET",
       url:"file.php",
       data:id,
       success:function(txt){
          $.prompt( txt,{ opacity: 0.2 });
       },
       // ... more ...
    }
});

jQuery 文档中有更多示例.

相关文章