【发布时间】:2011-10-16 07:10:06
【问题描述】:
想象一个网站,使用 jQuery .post()(或 .get())通过菜单加载内容。
想象一下,在这个动态加载的内容中应该是另一个 jQuery 帖子,以显示内容下的更多内容
(全部无需导航到地址栏中的新文件)
这就是我的意思(但只是基本框架...):
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<input type="text" name="n" id="n" placeholder="Search..." />
<button id="click1">Go</button>
<!-- the result of the search will be rendered inside this div -->
<br />
<div id="result" name="result"></div>
<script>
window.onload = (function(){
/* attach a submit handler to the button */
$("#click1").click(function(event) {
$.ajax({
url: 'test.php',
type: 'POST',
data: 'n: term',
success: function(msg) {
$( "#result" ).empty().append( msg );
}
});
});
});
</script>
</body>
</html>
所以,别笑了,我知道该页面只在 div 中加载自身,并且由于 ID 相同,这不起作用...它是 not Inception here ;-)
但即使我用新的 ID 将一个全新的页面链接到它,它也无法正常工作......
是否可以在现有的、已经加载了 jQuery AJAX 的内容中添加新的 jQuery AJAX?
我想是的,但我找不到我的错误......
/编辑
我想我需要提供更多意见:
test.php
<input type="text" name="n" id="n" placeholder="Search..." />
<button id="click1">Go</button>
<div id="result" name="result"></div>
<script>
window.onload = (function(){
$("#click1").click(function(event) {
$.ajax({
url: 'test2.php',
type: 'POST',
data: 'n: term',
success: function(msg) {
$( "#result" ).empty().append( msg );
}
});
});
$("#click2").click(function(event) {
$.ajax({
url: 'test3.php',
type: 'POST',
data: 'n2: term',
success: function(msg) {
$( "#result2" ).empty().append( msg );
}
});
});
});
</script>
test2.php
<input type="text" name="n2" id="n2" placeholder="Search..." />
<button id="click2">Go</button>
<div id="result2" name="result2"></div>
test3.php
<?php
echo 'It´s working!';
?>
我的最后一个问题:为什么它不起作用?为什么它不输出“它正在工作!”最后?
【问题讨论】:
-
您是否在萤火虫中检查了您的 ajax 调用实际上得到了响应?
-
是的,似乎第二个事件永远不会推送...(click2),它与第一个jQuery帖子一起加载...
标签: jquery html ajax post load