1

I am a little new to JQuery. let's suppose that we have this jquery function :

var f = $("#myForm");
var url = f.attr("action");
var formData = f.serialize();
$.post(url, formData, function(data) {
    $("#postResult").html(data);
});

and this form :

<form id="myForm" action="/Monitor/Test/FormPost" method="post">
    <div>First Name: <input name="FirstName" type="text" value="Bob" /></div>
    <div>Last Name: <input name="LastName" type="text" value="Cravens" /></div>
    <div>Age: <input name="Age" type="text" value="43" /></div>
    <input type="submit" value="Save Contact" />
    <div id="postResult">?</div>
</form>

How can I bind the save button with the jquery function ? Thank you

3 Answers 3

2

One simple way would be to bind to the click event of the button. Something like this:

$('#myForm input[type="submit"]').click(function () {
    var f = $("#myForm");
    var url = f.attr("action");
    var formData = f.serialize();
    $.post(url, formData, function(data) {
        $("#postResult").html(data);
    });
});

This specifically looks for the submit input that's a child of the form of id "myForm" (in case there are other buttons, etc.) and responds to its click event with the function in question.

Just to be safe, since you're essentially short-circuiting the form and posting via AJAX, you should also probably change the submit to a normal button:

<input type="button" value="Save Contact" />

Or perhaps:

<button value="Save Contact" />

Which would change your jQuery selector to:

$('#myForm input[type="button"]')

Or:

$('#myForm button')
Sign up to request clarification or add additional context in comments.

Comments

2
$(document).on("click","input[type=submit]",function(e)
        {
          e.preventDefault();
          var form = $(this).closest("form");
          $.post(form.attr("action",form.serialize(),function(d){
            //result
          });
        });

more general way.

6 Comments

one more question, what if I have many forms, how can I specify wich one i will be using, is there an other version where I can explicitly pick a form by it's Id ?
there can be many ways but you can seperate easily with dom manipulation for example; <form data-send="ajax" ....> and in jquery if(form.data("send") != undefined) {do with ajax} else {form.submit();}
@HamdiBaligh: In this example (which uses event delegation, which is awesome) the second argument is the selector for the button. If your form has an ID, you can use selectors like the one in my answer as the second argument in the call to .on(). You could also just put an id on your button, in which case the second argument to .on() (or the primary selector in my answer) would simply be #theIDOfTheButton.
@David I tried your solution but when I click the button it redirects me to an other html page where there is the String that I returned, I want it to be assynchronous
@HamdiBaligh: Sounds like the form is still posting normally (which doesn't mean the AJAX isn't working, just that it's being ignored by the primary form post). e.preventDefault() should prevent this. Adding a return false; in the event handler should help as well. I usually just recommend changing the button from a submit to a regular button, so its default action isn't to submit the form.
|
1
//this handler can work on any form that need to post all values
$("form input[type='submit']", f).click(function(e){
   e.preventDefault();
   var $this = $(this);
   var f = $this.parents('form');
   var url = f.attr("action");
   var formData = f.serialize();
   $.post(url, formData, function(data) {
       $("#postResult").html(data);
   });

   return false;
})

In this code you are subscribing click event. [e.preventDefault();][1] will stop your form from premature submittion and you can do the work you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.