Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit a form without submit button and without page refresh?

I have a form which I cannot change and I have to handle its submit function from jquery only.

The form have radio buttons only. I want to submit the form without the page refereshed when a radio button is checked.

Here is the code which i tried.

$('input[type="radio"]').click(function(){
    if ($(this).is(':checked'))
    {
      $('form#my-form').submit(function(){
        alert('form submitted');
      });
    }
});

It should alert the "form submitted" but it does not.

Any thing which I am doing wrong?

like image 491
Ahmad Avatar asked Sep 12 '12 11:09

Ahmad


2 Answers

If you want to submit the form without pageload, then you need to go for ajax.

If you want the alert while submitting, you need to modify ur code a bit..

$('input[type="radio"]').click(function(){
    if ($(this).is(':checked'))
    {
      $('form#my-form').submit();
    }
});

$('form#my-form').submit(function(){
        alert('form submitted');
      });
like image 91
sathish Avatar answered Oct 14 '22 09:10

sathish


I tried this example on jsFiddle. My code will submit the form on check of a radioButton.

$('input[type="radio"]').click(function(){
    if ($(this).is(":checked"))
      $('form#my-form').submit();
});​

If you'd like to alert anything on submit of your form you have to add this code

$("#my-form").submit(function() {
    alert("submitting form ...");        
});
like image 21
Pilgerstorfer Franz Avatar answered Oct 14 '22 09:10

Pilgerstorfer Franz