【问题标题】:jQuery Ajax: return value to caller?jQuery Ajax:返回值给调用者?
【发布时间】:2010-09-28 03:43:51
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: jquery ajax return-value return


    【解决方案1】:

    success 函数接受一个参数,该参数包含获取的数据。所以在你的例子中:

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

    更多示例在jQuery documentation

    【讨论】:

    • 如果我想从 php 文件中返回 HTML 数据(例如 var data = (success results)。上述是否也适用?
    【解决方案2】:
    jQuery.ajax({
         type:"POST",
         url:"file.php",
         data:"id1="+val1+"&id2="+val2,
         success:function(data){
           jQuery("#div_id").html(data);
        }
    

    【讨论】:

      猜你喜欢
      • 2010-11-16
      • 2011-09-21
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      • 2012-11-25
      相关资源
      最近更新 更多