【发布时间】:2014-03-14 20:38:23
【问题描述】:
我有一个带有此脚本的主页,用于将 status.html 加载到我的 info-box_main div 中
window.onload = ajax_req('scripts/home/php/status.html', 'info-box_main');
加载的status.html文件如下所示。
<div id="status_update">
<form id="status_form" method="post" action="/scripts/home/php/statusUpdate.php/">
<textarea name="status_update" placeholder="Type Link update here. " id="status_field" ></textarea>
<input type='submit' value='post'/>
</form>
</div>
<div id='status-box'>
<button onclick="myFunction('Harry Potter','Wizard')">Try it</button>
<script>
function myFunction(name,job){
alert("Welcome " + name + ", the " + job);
}
</script>
</div>
ajax_req 函数
function ajax_req(file_location, document_element)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} //ajax update request
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(document_element).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET", file_location ,true);
xmlhttp.send();
}
除了 javascript 一切正常。当我尝试单击按钮时,我收到一条错误消息,指出该功能未定义。任何帮助都是极好的!这只是测试 javascript...不是我需要的实际 javascript
【问题讨论】:
-
显示
ajax_req函数 -
ajax_req()函数是什么?它是如何工作的? -
我添加了这个功能。对此感到抱歉。
-
@user2930376 - 由于您标记了 jQuery,您可以使用
$.ajax()来加载文件。但是脚本仍然不会运行。
标签: javascript jquery html ajax