返回false在我的提交点击中不起作用 [英] return false is not working in my submit click

查看:90
本文介绍了返回false在我的提交点击中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用于Html.BeginForm的代码.

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;
                }
            });
        });

在其他情况下,警报消息也将其传给我的控制人后,我的return false无法正常工作吗?

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

谢谢你这样吗?

我这样尝试过

 $('#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..

谢谢

推荐答案

您要阻止表单在特殊条件下提交?

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

如果是这样,您应该首先避免使用click,而应使用支持键盘操作的submit事件.将事件更改为提交后,可以使用event.preventDefault()来阻止表单提交.

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!
                    }
                });
            });

此处的更多信息: .bind()jQuery API (在页面的下方,因此对.bind("submit"进行页内搜索).此处还有更多信息: .submit()

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

这篇关于返回false在我的提交点击中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆