验证程序致使为双击检查不当行为 [英] Validator causes improper behavior for double click check

查看:133
本文介绍了验证程序致使为双击检查不当行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从双击两次提交下面的脚本 prevent一个按钮。它工作正常。

但是,当我添加了一个必填字段验证场景变得笨拙。如果我尝试在文本框空值,校验大火提交。但是,当我在文本框中输入值,并尝试重新提交它不会做回发。

我理解的原因,在第一个按钮单击本身的变量isActionInProgress被设置为是,不论验证错误的。

什么是克服这一挑战的最佳方式?

标记

 < HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
<标题>< /标题>
<脚本类型=文/ JavaScript的SRC =htt​​p://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.1.js>< / SCRIPT>
<脚本类型=文/ JavaScript的>    $(文件)。就绪(函数(){        VAR isActionInProgress ='​​否';        $('。myButton的')。点击(函数(五){            如果(isActionInProgress =='否'){
                isActionInProgress =是;
            }
            其他{
                亦即preventDefault();
                //警报('STOP');
            }        });
    });< / SCRIPT>
< /头>
<身体GT;
<表ID =form1的=服务器>
< D​​IV>
    < ASP:文本框ID =txtEmpName=服务器>< / ASP:文本框>
    < ASP:按钮的ID =Button1的的CssClass =myButton的=服务器文本=提交的ValidationGroup =ButtonClick
        的OnClick =的button1_Click/>    < ASP:的RequiredFieldValidator ID =RequiredFieldValidator1的ControlToValidate =txtEmpName
        =服务器的ErrorMessage =的RequiredFieldValidator的ValidationGroup =ButtonClick文本=*>< / ASP:&的RequiredFieldValidator GT;
< / DIV>
< /表及GT;
< /身体GT;

参考


  1. 问题与Page_ClientValidate

  2. Hide在ASP.Net的ValidationSummary
  3. 冗余错误信息
  4. MSDN - ASP.NET验证的深度


解决方案

我觉得somthin是这样的:

  $('。myButton的')。点击(函数(五){
        如果(Page_IsValid){
        如果(isActionInProgress =='否'){
            isActionInProgress =是;
        }
        其他{
            亦即preventDefault();
            //警报('STOP');
        }
      }    });

I have following script to prevent a button from submitting twice by double clicking. It works fine.

But the scenario gets clumsy when I added a Required Field validator. If I try to submit with a blank value in textbox, the validator fires. But when I entered value in textbox and try to submit again it does not do a postback.

I understand the reason, in the first button click itself, the variable isActionInProgress is set as 'Yes' irrespective of the validation error.

What is the best way to overcome this challenge?

MARK UP

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.1.js"></script>
<script type="text/javascript">

    $(document).ready(function () {



        var isActionInProgress = 'No';

        $('.myButton').click(function (e) {

            if (isActionInProgress == 'No') {
                isActionInProgress = 'Yes';
            }
            else {
                e.preventDefault();
                //alert('STOP');
            }

        });


    });

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" CssClass="myButton" runat="server" Text="Submit" ValidationGroup="ButtonClick"
        OnClick="Button1_Click" />

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtEmpName"
        runat="server" ErrorMessage="RequiredFieldValidator" ValidationGroup="ButtonClick" Text="*"></asp:RequiredFieldValidator>
</div>
</form>
</body>

REFERENCES

  1. Problem with Page_ClientValidate
  2. Hide redundant error message in ASP.Net ValidationSummary
  3. MSDN - ASP.NET Validation in Depth

解决方案

I think somthin like this:

 $('.myButton').click(function (e) {
        if(Page_IsValid){
        if (isActionInProgress == 'No') {
            isActionInProgress = 'Yes';
        }
        else {
            e.preventDefault();
            //alert('STOP');
        }
      }

    });

这篇关于验证程序致使为双击检查不当行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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