简体   繁体   中英

Replace the current content of div with another page response

Sample code:

<html>
    <head>
        .....
    </head>
    <body>
        <div id="content">
            <form>
                ....
                <input type="submit" onclick="loadAnotherPage()"/>
            </form>
        </div>
    </body>
</html>  

I want to load another page say edit_profile.php in the content div. How can it be achieved without refreshing the page?

I tried to use jQuery load() method. But looks like I missed something. Any help would be highly appreciated.

When you click submit button it's submit form and page refresh that's why its not working.

Instead of type submit set button and then set function onclick .

function loadAnotherPage(){
    $("#content").load('edit_profile.php');
}
           $(document).ready(
            function() {
              $('#load_file').click(
            $.post('edit_profile.php',
             {},
             function(data){
                $("#content").html(data);
             },''
            )
               ); 
                }
                    );

Try this

<html>
 <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
 </head>
    <body>
     <div id="content"></div>
      <button id="loadpage">load new page</button>
<script>
 $(document).ready(function () {
    $("#loadpage").click(function() {
        $.get("edit_profile.php", function(response){
          $("#content").html(response);
        });
    })
 });
</script>
    </body>
</html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM