admin管理员组

文章数量:1318147

I've got the following code that I use on my links. This prevents the page from reloading and loads the content from the href tag in a div.

  $("a[rel='right']").click(function(e){
    e.preventDefault();
    pageurl = $(this).attr('href');
    $.ajax({url:pageurl.replace('index.php', 'rightcolumn.php')+'&rel=right',success: function(data){
      $('#WMS_NEW_right').fadeOut(500, function(){ $('#WMS_NEW_right').html(data).fadeIn(1000); }); 
    }
    });  
    if(pageurl!=window.location){
      window.history.pushState({path:pageurl},'',pageurl);
    }
    return false;
  });
});

My Question: I need the use the same concept behind this, except on form submit, it needs to not reload the page, but submit the form only inside a div #WMS_NEW_right. How can I do this? I don't need push state or anything, just need to be able to control that form with class="formrelright" to only reload a div and get the url from the form action. I will also need all data from the form method="POST" on the new page (inside div)

I've got the following code that I use on my links. This prevents the page from reloading and loads the content from the href tag in a div.

  $("a[rel='right']").click(function(e){
    e.preventDefault();
    pageurl = $(this).attr('href');
    $.ajax({url:pageurl.replace('index.php', 'rightcolumn.php')+'&rel=right',success: function(data){
      $('#WMS_NEW_right').fadeOut(500, function(){ $('#WMS_NEW_right').html(data).fadeIn(1000); }); 
    }
    });  
    if(pageurl!=window.location){
      window.history.pushState({path:pageurl},'',pageurl);
    }
    return false;
  });
});

My Question: I need the use the same concept behind this, except on form submit, it needs to not reload the page, but submit the form only inside a div #WMS_NEW_right. How can I do this? I don't need push state or anything, just need to be able to control that form with class="formrelright" to only reload a div and get the url from the form action. I will also need all data from the form method="POST" on the new page (inside div)

Share Improve this question asked Dec 28, 2013 at 21:01 kdjernigankdjernigan 3972 gold badges7 silver badges22 bronze badges 1
  • try to use html5 history api that would give you a soln – NaveenThally Commented Nov 7, 2014 at 2:46
Add a ment  | 

3 Answers 3

Reset to default 5

From my understanding, you want to use ajax to post a form without reloading the page during the form submission. So I would consider the following:

$('.formrelright').submit(function(event) {
    event.preventDefault();

    $.ajax({
        url: url,
        type: 'POST',
        data: $(this).serialize(),
        success: function(data) {
            // Whatever you want
        }
    });
});

Either use target and an IFrame, or JQuery to submit the form in the background. The latter is preferable if you want to use the contents of the response.

JQuery post()

Maybe try it like this:

HTML:

<!-- Notice there is no 'form' element -->
<div id="myform">
    <input type="text" name="firstName"><br>
    <input type="text" name="lastName"><br>
    <button type="button" id="submit_myform">Submit</button>
</div>

<div id="resultArea"></div>

<!-- the biggest critique about this method might be that
     yes, it is not very semantic.  If you want to use a form,
     just throw in an 'e.preventDefault()' in your jQuery -->

jQuery:

$('#submit_myform').on('click',function() {

    var firstName = $('input[name="firstName"]').val(),
        lastName = $('input[name="lastName"]').val();

    $.ajax({
        type: "POST",
        url: 'form.php',
        data: {
            firstname: firstName,
            lastname: lastName
        },
        //the success function is automatically passed the XHR response
        success: function(data) {
            $('#resultArea').html(data);
        },
    });

});

本文标签: