在ASP.NET代码中使用javascript重置Jquery验证后停止回发

时间:2015-10-01 12:13:13

标签: javascript jquery asp.net postback

我在该表单中有一个表单,有两个按钮和两个文本框,点击第一个文本框字段后,第一个文本框字段显示第一个文本框字段后,第一个文本框的验证消失,但回帖后。

如何在重置jquery验证时阻止回发。

      <form id="form1" runat="server">

                    <div id="div1" class="form-horizontal">
                                            <div class="form-group">
                                                <div class="col-md-3">
                                                    <asp:TextBox  ID="TextBox1" runat="server"></asp:TextBox>  
                                                </div>

                                            </div>
                                            <div class="form-group">
                                                <div class="col-md-3">

                                                    <asp:Button ID="Button1"  CssClass="submit btn btn-primary " runat="server" Text="Submit" />
                                                </div>
                                            </div>
                                        </div>


                            <div id="div2" class="form-horizontal">
                                            <div class="form-group">
                                                <div class="col-md-3">

                                                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                                                     </div>
                                            </div>
                                            <div class="form-group">
                                                <div class="col-md-3">
                                                    <asp:Button ID="Button2" CssClass="btn btn-primary" runat="server" Text="Submit" />
                                                </div>
                                            </div>
                                        </div>
                </form>




<script>
        $(document).ready(function () {
            $('#Button1').click(function () {
                $('#form1').validate({
                    errorClass: 'help-block animation-slideDown',
                    errorElement: 'div',
                    errorPlacement: function (error, e) {
                        e.parents('.form-group > div').append(error);
                    },
                    highlight: function (e) {
                        $(e).closest('.form-group').removeClass('has-success has-error').addClass('has-error');
                        $(e).closest('.help-block').remove();
                    },
                    success: function (e) {
                        e.closest('.form-group').removeClass('has-success has-error');
                        e.closest('.help-block').remove();
                    }, 
                         rules: {
                             'TextBox1': {
                                required: true,
                            },
                        },
                        messages: {
                            'TextBox1': {
                                required: 'ProvideYourCurrentPassword',
                            },
                        },
                });
                $("#TextBox2").rules('remove');
            });
            $('#Button2').click(function () {
                $('#form1').validate({
                    errorClass: 'help-block animation-slideDown',
                    errorElement: 'div',
                    errorPlacement: function (error, e) {
                        e.parents('.form-group > div').append(error);
                    },
                    highlight: function (e) {
                        $(e).closest('.form-group').removeClass('has-success has-error').addClass('has-error');
                        $(e).closest('.help-block').remove();
                    },
                    success: function (e) {
                        e.closest('.form-group').removeClass('has-success has-error');
                        e.closest('.help-block').remove();
                    },

                    rules: {
                             'TextBox2': {
                                required: true,
                            },
                        },
                        messages: {
                            'TextBox2': {
                                required: 'ProvideYourCurrentPassword',
                            },
                        },
                });
                $("#TextBox1").rules('remove');
            });
        });
    </script>

2 个答案:

答案 0 :(得分:1)

试试这个脚本,它可能对你有帮助。

<script>
        $(document).ready(function () {
            $('#form1').validate({
                errorClass: 'help-block animation-slideDown',
                errorElement: 'div',
                errorPlacement: function (error, e) {
                    e.parents('.form-group > div').append(error);

                },
                highlight: function (e) {
                    $(e).closest('.form-group').removeClass('has-success has-error').addClass('has-error');
                    $(e).closest('.help-block').remove();
                },
                success: function (e) {
                    e.closest('.form-group').removeClass('has-success has-error');
                    e.closest('.help-block').remove();
                },
            });
            $('#Button1').click(function () {
                $("#TextBox1").rules("add", { required: true, messages: { required: '<%=ValidationMessage.ProvideYourCurrentPassword%>' } });
                $("#TextBox2").each(function () {
                    $(this).rules('remove');
                });
            });


            $('#Button2').click(function () {
                $("#TextBox2").each(function () {
                    $(this).rules('add', {
                        required: true
                    });
                });
                $("#TextBox1").each(function () {
                    $(this).rules('remove');
                });
            });

        });
    </script>

答案 1 :(得分:0)

Sys.Application.add_load

中发布您的Jquery代码
$(function(){
            Sys.Application.add_load(function () { 
           // your Java script Code here
                   });

            });