jQuery.post()里面的jQuery.post()

Seb*_*eba 10 html ajax jquery post load

想象一个网站,通过使用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>
Run Code Online (Sandbox Code Playgroud)

所以,别笑了,我知道页面只在div中加载自己,因为ID相同而无法正常工作......这不是Inception ;-)

但即使我用新的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>
Run Code Online (Sandbox Code Playgroud)

test2.php

<input type="text" name="n2" id="n2" placeholder="Search..." />
<button id="click2">Go</button>
<div id="result2" name="result2"></div>
Run Code Online (Sandbox Code Playgroud)

test3.php

<?php
echo 'It´s working!';
?>
Run Code Online (Sandbox Code Playgroud)

我的最后一个问题:为什么它不起作用?为什么不输出"它正在工作!" 在末尾?

Dar*_*rcy 12

你可以在第一篇文章的成功处理程序中添加另一篇文章:

$("#click1").click(function(event) {
    $.ajax({
        url: 'test.php',
        type: 'POST',
        data: 'n: term',
          success: function(msg) {
            $( "#result" ).empty().append( msg );

            $.ajax({/* params here */});
          }
     });
});
Run Code Online (Sandbox Code Playgroud)