使用单击按钮的ID运行AJAX请求
Run AJAX request using ID from button clicked
为了对几个不同的文件使用相同的AJAX请求,我有一个AJAX请求,它使用按钮的ID来选择所需的下一个文件。
按钮看起来是这样的:
<button type="button" id="question_1" onclick="nextQuestion(this.id)">Start the challenge</button>
AJAX请求的开头有一个按钮ID作为参数:
function nextQuestion(buttonID)
当按钮被按下时,buttonID变量被收集:
var splitID = buttonID.split('_'); //split the ID from the end of the button
var questionID = splitID.pop();
然后使用这个ID生成请求的文件名。
xmlhttp.open("GET","question" + questionID + ".php",true); // concatenate button ID to filename
由于某种原因,即使我在另一个区域工作,这也不起作用。我想不出我做错了什么。完整的AJAX请求是:
function nextQuestion(buttonID){
quizScore = 0;
var xmlhttp;
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("gameWrapper").innerHTML=xmlhttp.responseText;
}
}
var splitID = buttonID.split('_'); //split the ID from the end of the button
var questionID = splitID.pop();
xmlhttp.open("GET","question" + questionID + ".php",true); // concatenate button ID to filename
}
缺失xmlhttp.send(null);
函数
试题:
function nextQuestion(buttonID){
quizScore = 0;
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var splitID = buttonID.split('_'); //split the ID from the end of the button
var questionID = splitID.pop();
xmlhttp.open("GET","question" + questionID + ".php",true); // concatenate button ID to filename
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("gameWrapper").innerHTML=xmlhttp.responseText;
}
};
xmlhttp.send(null);//send request
}
相关文章:
- 运行AJAX脚本
- 在弹出函数中运行AJAX
- Javascript.在当前AJAX停止后运行AJAX流
- 运行 AJAX 需要什么 - 将 jQuery 添加到页面处理 AJAX 调用
- 如何先追加,然后在jquery中运行ajax
- 在document.ready jquery上运行ajax请求
- 运行 ajax 函数以每 3 秒检查一次 json 响应
- 运行 AJAX 函数的多个实例 - 仅第二个实例更新
- 使用 JQuery 运行 AJAX 和使用普通 XMLHttpRequest 运行 AJAX 有什么区别?
- 一个接一个地运行 ajax
- 如何排序这个jQuery tha运行ajax然后必须返回true
- 运行 AJAX 异步时文档类型标记的“意外输入结束”,但在同步时则不然
- 在页面加载时运行ajax,然后每5秒运行一次
- 阻止对运行AJAX的超链接执行默认操作
- 在运行ajax请求时,无法将内部html设置为null
- 运行AJAX调用的javascript代码
- 在因检测Adblocker(Ghostery)而被阻止的URL上运行Ajax请求
- 当鼠标(或手指)不再拖动时,我怎么能只在更改时运行AJAX调用呢
- 在运行AJAX调用之后,不会执行jquery代码
- 在点击事件上运行AJAX调用——使用React.js