douni9620 2012-07-03 12:29
浏览 110
已采纳

如何在没有onclick / onkeyup / onchange等的情况下调用Ajax函数

I am trying to include a .php file that will generate a form for me by picking out questions from a MySQL database. I've found numerous ways to call this function that I have called getQuestions() with 1 parameter (page number). I've seen drop down menus that will execute my php file using onchange, or buttons (onclick) etc. But is there a way to use my function without these? I just want it to automatically load with the rest of the page without any unnecessary interactions.

Don't know if this matters but my page is made in jQuery Mobile so it should only be visible when the correct page/div-id is active.

function getQuestions(str)
    { 
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("page_1").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","generate_questions.php?p="+str,true);
    xmlhttp.send();
    }

These are the functions I have tried to use to view the .php within the rest of the HTML (without success)

<script type="text/javascript">
                    $(document).ready(function() {
                    getQuestions(1);
                    });
                    </script>


window.onload = function() {
                    $("#page_1").load(getQuestions(1));
                    }

window.onload = function() {
                    getQuestions(1);
                    }
  • 写回答

4条回答 默认 最新

  • duanrong6802 2012-07-04 13:08
    关注

    Seems I solved my problem by assigning the php code to a smaller div than originally planned. I can't assign it to the page id, but to a smaller div within the page. The odd thing is it seems I can't really generate new div's within the php code either, so that sort of limits it's usage.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?