我已经多次搜索这个问题的答案,但尚未找到类似的问题或答案。如果这是“重新询问”,我深表歉意。
本质上我的问题是这样的:我的父页面中有一个 iframe,iframe 中有一个 php 页面,其中包含我的 .js 文件,其中包含我的 AJAX。我希望单击 iframe 中的按钮来创建 AJAX 请求并更新 iframe。据我所知,我已经正确设置了所有内容,但是当我尝试回显 $_POST (我也尝试过 GET)变量来测试它们时,它们永远不会改变。这是我的代码:
父级:
<!DOCTYPE html>
<html style = "height: 100%; width: 100%;">
<head>
<title>Employee Web Service</title>
<link rel="stylesheet" type="text/css" media="screen, projection" href="lib/css/main.css" />
<script src="lib/scripts/jquery-2.0.3.min.js" type="text/javascript"></script>
</head>
<body style = "height: 100%;">
<div id = "header">
<img src = "images/header.png" id = "headerLogo">
</div>
<div id = "nav">
<ul>
<li><a href = "home.php" target = "content">Home</a></li>
<li><a href = "#">Modules</a>
<ul>
<li><a href = "test.php?id=1" target = "content">Module 1</a></li>
<li><a href = "test.php?id=2" target = "content">Module 2</a></li>
<li><a href = "test.php?id=3" target = "content">Module 3</a></li>
<li><a href = "test.php?id=4" target = "content">Module 4</a></li>
</ul>
</li>
<li><a href = "scores.php" target = "content">Scores</a></li>
<li><a href = "calendar.php" target = "content">Calendar</a></li>
<li><a href = "logout.php">Logout</a></li>
</ul>
</div>
<div id = "content">
<iframe src = "home.php" name = "content" id = "frameContent"></iframe>
<script>
$(document).ready(function(){
$('#frameContent').load(function(){
$('#frameContent').contents().find('head').append('<link href="lib/css/iframe.css" rel="stylesheet" type="text/css" />');
});
});
</script>
</div>
</body>
</html>
Iframe 内的 test.php:
<?php
if(isset($_POST['begin'])){
$test = true;
echo($_POST['begin']);
} else if (isset($_POST['backToSlides'])){
echo($_POST['backToSlides']);
}
?>
<head>
<script src="lib/scripts/jquery-2.0.3.min.js" type="text/javascript"></script>
<script src = "lib/scripts/testScript.js" type="text/javascript"></script>
</head>
<body>
<div id = "startTest" <?php if(isset($test)){echo 'style = "visibility: hidden; disabled: true;"';}?> >
<h1>Test <?php echo $id; ?></h1>
<form id = "begin">
<input type = "submit" name = "backToSlides" value = "Go back" style = "background: #B9090B;">
<input type = "submit" name = "begin" value = "Start Test" style = "background: #09913A;">
</form>
</div>
<div id = "testContent"></div>
</body>
testScript.js:
$(document).ready(function(){
$('input[name = "backToSlides"], input[name = "begin"]').click(function(event){
event.preventDefault();
post = $(this).attr("name") + "=" + $(this).val();
$.ajax({
url: "/test.php",
type: "POST",
data: post, //Data purposely not serialized due to the way submit-type input field values are handled
success: function(){
console.log("Processing request...");},
error: function(){
console.log("Error submitting request");}
});
});
});
这总是调用成功函数,但我从未在发布数据中看到提交按钮的值。我知道它们被正确地传递到ajax调用中,因为我尝试调用alert($(this).attr("name") + "="+ $(this).val())并且我得到了预期的结果“键=值”的结果。
非常感谢您的快速回复!
最佳答案
您需要阻止默认浏览器提交表单,否则由于您提交到相同的网址,它只会刷新 iframe 页面
$('input[name = "backToSlides"], input[name = "begin"]').click(function(event){
event.preventDefault();
/* ajax */
})
关于javascript - iframe 中的 Ajax 不更新 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24622021/