Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return false is not working in my submit click

Tags:

jquery

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

like image 423
kumar Avatar asked Jan 23 '26 04:01

kumar


2 Answers

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()

like image 157
mqchen Avatar answered Jan 24 '26 18:01

mqchen


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

you should probably do something like this:

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

 event.preventDefault();
});
like image 22
Mihai Toader Avatar answered Jan 24 '26 18:01

Mihai Toader



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!