jQuery验证和更新面板一起 [英] Jquery Validation And Update Panel Together
问题描述
我正在使用jQuery Validation Plugin 1.8.1.它可以按预期正常工作,但是当我在内容页面的一个中添加了一个更新面板时,问题开始了.
I am Using jQuery Validation Plugin 1.8.1.It's working perfectly as expected but the problem started when I added an update panel in one on my content pages.Here I have added
$(function () {
//Code that runs before update panel is fired.
//Listen for update panel
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
//Re-initialize jquery after an auto post back.
function EndRequestHandler(sender, args) {
Validate();
}
});
在我的更新面板中,这样在母版页中编写的每个部分回发中调用我的Validate方法
In side my update Panel to call my Validate method at every Partial Post Back which is written in the Master Page like this
<script type="text/javascript">
$().ready(function() {
Validate();
});
</script>
<script type="text/javascript">
function Validate();
{
var container = $('div.container');
// validate the form when it is submitted
var validator = $("#form2").validate({
errorContainer: container,
errorLabelContainer: $("ul", container),
wrapper: 'li',
meta: "validate"
});
}
</script>
只需添加此更新面板,就可以看到当我按下提交"按钮表单时,它会进行验证并显示所需的消息几乎2秒钟,然后提交表单. 解决方法是什么?
Just by adding this Update Panel I can see when I press the Submit button form it validates and shows the required message for hardly 2 seconds and then submits the form. What is the workaround for this?
推荐答案
订阅initializeRequest事件,并检查事件处理程序的格式是否有效.如果不是,则取消回发:
Subscribe for the initializeRequest event and check in that event handler is form valid. If not the cancel postback:
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(instance_initializeRequest);
function instance_initializeRequest(sender, args) {
if (!$("#form2").validate().form()) {
args.set_cancel(true);
}
}
这篇关于jQuery验证和更新面板一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!