4

Following is the form

@using (Html.BeginForm(MVC.CollectionPlanConfirmation.ActionNames.Edit, MVC.CollectionPlanConfirmation.Name, FormMethod.Post, new { @id = "collectionPlanForm" }))
{
.................
}

Submit button is outside Form. Following is the submit button on my MVC view.

<input type="submit" value="Save" class="aSubmit" onclick="return SubmitForm();" />

Following is the jquery that should submit the form.

<script type="text/javascript">    
function SubmitForm() {
...... validation code........
$('#collectionPlanForm').submit();
}
</script>

I could see, IE8, and Firefox submitting the form, but it doesn't go well with Chrome. It doesn't do anything at all.

What could be the reason, and how to fix it?

Any help on this, much appreciated.

Thanks

Nirman
  • 6,715
  • 19
  • 72
  • 139
  • 1
    You should be binding the `SubmitForm` function to the form's `submit` event. – Blender Jul 02 '13 at 06:24
  • 1
    Is there any error ? As I think its working with chrome http://jsfiddle.net/g8QVc/ and second this submit button automatically call the form submit. – pixelbyaj Jul 02 '13 at 06:35
  • 1
    the submit button is outside the form.. and hence I am submitting it manually. Please see the edit in question. – Nirman Jul 02 '13 at 06:44

3 Answers3

3

The best way to perform a submit for jquery is in this way.

$('#submitButton').click( function() {
    $.ajax({
        url: 'some-url',
        type: 'post',
        dataType: 'json',
        data: $('form#myForm').serialize(),
        success: function(data) {
             console.log("success");
        }
    });
});

http://jsfiddle.net/BD3Rt/

Always nice to keep html clean and separate your jquery/javascript code.

GenericJon
  • 8,746
  • 4
  • 39
  • 50
Sanath
  • 4,774
  • 10
  • 51
  • 81
  • this is partially working for me... that means, I am now able to submit the form.. but if the server-side validations gets failed, I have to display the same form with error messages printed on screen, otherwise want to redirect to another page. This is currently being handled in MVC code using "return View", and "return RedirectToAction(...)", respectively. – Nirman Jul 02 '13 at 07:11
2

Try this:

HTML

<input type="button" value="Save" class="aSubmit" />

SCRIPT

$(function () {
    $(document).on('click', '.aSubmit', function () {
        $('#collectionPlanForm').submit();
    });
});

DEMO FIDDLE

Olrac
  • 1,527
  • 2
  • 10
  • 22
1

put ID for form and use $("#yorID").submit() and you can see the example here example