kumar
kumar

Reputation: 2944

return false is not working in my submit click

this is the code i am using for my Html.BeginForm..

 $('#PbtnSubmit').click(function() {
            $('#PricingEditExceptions input[name=PMchk]').each(function() {
                if ($("#PricingEditExceptions input:checkbox:checked").length > 0) {
                    var checked = $('#PricingEditExceptions input[type=checkbox]:checked');
                    var PMstrIDs = checked.map(function() {
                        return $(this).val();
                    }).get().join(',');
                    $('#1_exceptiontypes').attr('value', exceptiontypes)
                    $('#1_PMstrIDs').attr('value', PMstrIDs);
                } else {
                    alert("Please select atleast one exception");
                    return false;
                }
            });
        });

in else blcok my return false is not working after alert mesage also its going to my controler?

thanks is this right?

i tried like this

 $('#PbtnSubmit').click(function() {
                $('#PricingEditExceptions input[name=PMchk]').each(function() {
                    if ($("#PricingEditExceptions input:checkbox:checked").length > 0) {
                        var checked = $('#PricingEditExceptions input[type=checkbox]:checked');
                        var PMstrIDs = checked.map(function() {
                            return $(this).val();
                        }).get().join(',');
                        $('#1_exceptiontypes').attr('value', exceptiontypes)
                        $('#1_PMstrIDs').attr('value', PMstrIDs);
                    } else {
                        alert("Please select atleast one exception");

                    }
                });
return false;
            });

if i do like this on submit its doing nothing..

thanks

Upvotes: 2

Views: 19451

Answers (4)

pablo bragan
pablo bragan

Reputation: 1

Sometimes you have to clear previous event binding and then bind the event.

Using, jQuery, this would be something like

 $("#...").off('click');$("#...").click(function(){..});

Upvotes: 0

lambacck
lambacck

Reputation: 9926

If you have an error (uncaught exception) in your submit handler, you won't end up returning false and stopping the event propagation. If you switch to using the event.preventDefault() method as suggested by the other responders and you are still having trouble, then you should call event.preventDefault right at the beginning of your submit handler so that you know that the page won't submit on you before you get a chance to debug your error.

Upvotes: 2

mqchen
mqchen

Reputation: 4193

You want to prevent the form from submitting on special conditions?

If so, you should first probably avoid using click and rather use the submit event which supports keyboard actions. After changing the event to submit, you can use event.preventDefault() which prevents the form submit.

So your code should probably look like this (note: untested):

$('<your form selector>').bind('submit', function(event) {
                $('#PricingEditExceptions input[name=PMchk]').each(function() {
                    if ($("#PricingEditExceptions input:checkbox:checked").length > 0) {
                        var checked = $('#PricingEditExceptions input[type=checkbox]:checked');
                        var PMstrIDs = checked.map(function() {
                            return $(this).val();
                        }).get().join(',');
                        $('#1_exceptiontypes').attr('value', exceptiontypes)
                        $('#1_PMstrIDs').attr('value', PMstrIDs);
                    } else {
                        alert("Please select atleast one exception");
                        event.preventDefault(); // Stop the submit here!
                    }
                });
            });

More info here: .bind() jQuery API (it's quite far down the page, so do an in-page search for .bind("submit"). More here as well: .submit() and .preventDefault()

Upvotes: 8

Mihai Toader
Mihai Toader

Reputation: 12243

Check this: http://api.jquery.com/event.preventDefault/

you should probably do something like this:

$('#PbtnSubmit').click(function(event) {
 /// code 

 event.preventDefault();
});

Upvotes: 3

Related Questions