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

标签 php jquery mysql ajax jqmobi

我正在尝试包含一个 .php 文件,该文件将通过从 MySQL 数据库中挑选问题来为我生成一个表单。我发现了很多调用这个函数的方法,我用 1 个参数(页码)调用了 getQuestions()。我已经看到下拉菜单将使用 onchange 或按钮 (onclick) 等执行我的 php 文件。但是有没有办法在没有这些的情况下使用我的函数?我只想让它自动加载页面的其余部分,而无需任何不必要的交互。

不知道这是否重要,但我的页面是在 jQuery Mobile 中制作的,因此它应该只在正确的页面/div-id 处于事件状态时可见。

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();
    }

这些是我试图用来查看其余 HTML 中的 .php 的函数(没有成功)

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


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

window.onload = function() {
                    getQuestions(1);
                    }

最佳答案

您可以使用文档就绪事件来加载其余内容:

$(document).ready(function() {
   // your code
 });

关于php - 如何在没有 onclick/onkeyup/onchange 等的情况下调用 Ajax 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11310923/

相关文章:

mysql - 每天计算和总结不同的值

php - 从按钮发送数据到 php 页面并发布结果

php - 传递来自 Laravel 的状态代码未被 Angular http 接收

javascript - 使用 jQuery 更新 HTML 元素样式参数值

javascript - 与 jQuery 在同一页面中的图像旋转器

mysql - 简单的 SQL 语句耗时过长

php - 选择最后一个和下一个记录时 Symfony DQL 语法错误

PHP array_unique 返回重复项

php - 如何在cms页面调用phtml文件设置页面标题

php - 如何在div中提交表单之前使用jquery显示上传图像预览