Javascript 将Ajax结果加载到主(父)页面';s分区
在主页中,有一个id为“搜索结果”的Javascript 将Ajax结果加载到主(父)页面';s分区,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,在主页中,有一个id为“搜索结果”的以显示所有搜索结果。搜索结果是通过ajax调用加载的。结果(子页)直接放入div <script> $(document).ready(function () { $("#update_op").click(function(event){ event.preventDefault(); var link = $(this).attr("href"); // Ajax here
以显示所有搜索结果。搜索结果是通过ajax调用加载的。结果(子页)直接放入div
<script>
$(document).ready(function () {
$("#update_op").click(function(event){
event.preventDefault();
var link = $(this).attr("href");
// Ajax here
$.ajax({
type: "POST",
url: link,
dataType: "json",
success:function(data) {
$("#search_results").html(data);
}
});
return false; // for good measure
});
});
</script>
<div class="div_table">
<div id="item_row_4" class="div_row">
<div class="div_cell">
<a id="update_op" class="update_op op" href="http://abc/bk/v/show-update/id/4">update</a>
</div>
</div>
</div>
在子页面中,我可以捕获click事件并执行另一个ajax
调用以接收结果。我想将结果加载回主页的div
“搜索结果”
。我不能
这是主页的部分html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<title>search</title>
......
</head>
<body>
......
<div id="search_results"></div>
</body>
</html>
$(“#搜索结果”).html(数据)代码>此行未加载我收到的结果
解决方案:将数据类型更改为html,它可以正常工作。我遇到了这个问题,下面是我的解决方案:
<script>
$(document).ready(function () {
$("#update_op").click(function(event){
event.preventDefault();
var link = $(this).attr("href");
// Ajax here
$.ajax({
type: "POST",
url: link,
dataType: "json",
success:function(data) {
location.reload();
}
});
return false; //for good measure
});
});
</script>
$(文档).ready(函数(){
$(“#更新操作”)。单击(函数(事件){
event.preventDefault();
var link=$(this.attr(“href”);
//这里是阿贾克斯
$.ajax({
类型:“POST”,
网址:link,
数据类型:“json”,
成功:功能(数据){
location.reload();
}
});
返回false;//用于良好度量
});
});
我希望这能有所帮助。我不知道你为什么要使用“数据类型”JSON,你没有发送JSON,所以你不应该把代码放进去
下面是一个文件名为“probando.php”的示例。这对我有好处
<body>
<div id="search_results"></div>
<br /><br />
<div class="div_table">
<div id="item_row_4" class="div_row">
<div class="div_cell">
<a id="update_op" class="update_op op" href="probando.php">update</a>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#update_op").click(function(event){
event.preventDefault();
var link = $(this).attr("href");
// Ajax here
$.ajax({
url: link,
type: 'POST',
success: function (response){
$("#search_results").html(response);
}
});
});
});
</script>
$(文档).ready(函数(){
$(“#更新操作”)。单击(函数(事件){
event.preventDefault();
var link=$(this.attr(“href”);
//这里是阿贾克斯
$.ajax({
网址:link,
键入:“POST”,
成功:功能(响应){
$(“#搜索结果”).html(回复);
}
});
});
});
和“probando.php”代码:
运行时dom加载元素的单击事件必须以这种方式编写
$("#update_op").on('click',function(event){
e.preventDefault(); // return false
$.ajax({
url: "url to fetch result",
type: 'POST'
}).done(function(data){
$("#search_results").html(data); // this would change the data, you could use append(), prepend() etc. you would have to process data to display with css if needed
});
});
也许您想更改为数据类型:“html”
或数据类型:“text”
?将JSON对象附加到div没有多大意义。看起来这是我的错误。我把它改成html。现在似乎在工作。
$("#update_op").on('click',function(event){
e.preventDefault(); // return false
$.ajax({
url: "url to fetch result",
type: 'POST'
}).done(function(data){
$("#search_results").html(data); // this would change the data, you could use append(), prepend() etc. you would have to process data to display with css if needed
});
});