加载页面后立即在textarea中显示ajax响应

时间:2010-06-24 06:21:35

标签: jquery ajax django

我有一个连续的ajax请求,要求提供一些要在textarea中显示的数据。现在我的问题是,如何在加载页面后立即显示数据。

My template code is as follows:




     <html>
<head>
<script type="text/javascript" src="/jquerycall/"></script>
<script type="text/javascript">
     $(document).ready(function()
     {
               var setid = 0;
               var s = new String();
               var my_array = new Array();
               var no_of_lines = 0;
               var array_copy = new Array();
               var fname ="{{fname}}"
               var pid = "{{pid}}"
               function displayfile()
               {

                var text = $("#textid").val();
                if(text == "")
                {
                         no_of_lines = 0;
                }
                else
                {

                        array_copy = text.split("\n");
                        no_of_lines = array_copy.length;


                }
                /*$("#tid").val(no_of_lines)*/
                $.ajax({
                                type:"POST",
                                url:"/displayoutput/",
                                datatype:"json",
                                data:{text:no_of_lines,filename:fname,pid:pid},
                                success:function(data)
                                        {

                                                s = data.filedata;
                                                if(s == "")
                                                {
                                                   clearInterval(setid);

                                                }
                                                my_array = s.split("\n");
                                                displaydata(my_array);

                                        }


                        });


              }

                    function displaydata(my_array)
              {

                   var i = 0;
                   var length = my_array.length;
                   for(i=0;i<my_array.length;i++)
                   {
                       var line = my_array[i] + "\n";
                       line = line.replace(/</g,"&lt;");
                       $("#textid").append(line);
                       $("#tid").val(line)

                   }


             }


              $("form").submit(function(event)
              {

                    setid = setInterval(displayfile,1000);
                    event.preventDefault();

              });

              $("#stop").click(function()
              {
                    clearInterval(setid);
              });
     });

</script>
</head>
<body>
<form method="post">
<input type="submit" value="Click Me"/><br><br>
<button type="button" id="stop">Stop</button><br><br>
<textarea id="textid" disabled="true" readonly="true" rows=30 cols=100></textarea><br><br>


</form>
</body>
</html>



Here, the data is loaded when I click the "Click Me"
But I want the data to be loaded as soon as page is loaded
Any suggestions will be valuable
Thanks in advance 

tazim

2 个答案:

答案 0 :(得分:0)

只需在displayfile();处理程序中调用.ready()

如果您不等一秒钟,请修改您的代码:

$("form").submit(function(event)
{
       setid = setInterval(displayfile,1000);
       event.preventDefault();
}).submit();

或者......只需将displayfile();放在'.ready handler to execute that ajax request as soon as possible (after the DOM`中的任何位置即可;

如果您不需要DOM准备就绪,您可以通过创建自动执行匿名函数来进一步执行该功能。

(function(){
    // code to execute
})();

您可以使用displayfile()函数中的代码填写它,然后执行 的尽快

答案 1 :(得分:0)

tazim,

变化:

          $("form").submit(function(event)
          {
                setid = setInterval(displayfile,1000);
                event.preventDefault();
          });

为:

          $(document).ready(function() {
                displayfile();
                setid = setInterval(displayfile,1000);
          });

吉姆