这是我用于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我的返回false是不能正常警报消息之后它也会进入我的控制器?
谢谢,这是对的吗?
我试过这个
$('#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;
});
如果我这样做就提交它什么都不做..
感谢
答案 0 :(得分:8)
您想阻止表单在特殊情况下提交吗?
如果是这样,您应该首先避免使用click
,而是使用支持键盘操作的submit
事件。更改要提交的事件后,您可以使用event.preventDefault()
来阻止表单提交。
所以你的代码可能看起来像这样(注意:未经测试):
$('<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()和.preventDefault()
答案 1 :(得分:3)
请检查:http://api.jquery.com/event.preventDefault/
你应该做这样的事情:
$('#PbtnSubmit').click(function(event) {
/// code
event.preventDefault();
});
答案 2 :(得分:2)
如果提交处理程序中有错误(未捕获的异常),则最终不会返回false并停止事件传播。如果您按照其他响应者的建议切换到使用event.preventDefault()方法并且您仍然遇到问题,那么您应该在提交处理程序的开头调用event.preventDefault,以便您知道页面不会在你有机会调试错误之前提交给你。
答案 3 :(得分:0)
有时您必须清除以前的事件绑定,然后绑定事件。
使用,jQuery,这就像
$("#...").off('click');$("#...").click(function(){..});