jquery numberOfInvalids() 函数不起作用

发布于 2024-12-22 12:02:49 字数 6702 浏览 2 评论 0原文

我正在使用 jquery 验证插件 1.9.0 。我从这里下载了该插件: http://bassistance.de/jquery-plugins/ jquery-plugin-validation/

我已从此链接复制以下代码 http://docs.jquery.com/Plugins/Validation/Validator/numberOfInvalids 找出我的表格中的错误数量。但它不起作用。

$(document).ready(function() {
    $('a.delete').click(function() {
        invalidHandler: function() {
            $("#summary").text(validator.numberOfInvalids() + " field(s) are invalid");
        }
    });
});     

然后

<div id="summary" class="summary"> </div>
<a href="#" class="delete">click</a>

我的表单头看起来像这样:

<form class="uniform"  autocomplete="off" id="myForm" enctype="multipart/form-data" action="<?echo base_url();?>addteacher_salary/add " method="post" name="myForm">

我不在表单中调用任何验证函数,我只是在 head 标签内加载验证插件,并且验证有效。

你能告诉我代码有什么问题吗?

编辑

我的完整表格如下所示:

   <form class="uniform"  autocomplete="off" id="myForm" enctype="multipart/form-data" action="<?echobase_url();?>addteacher_salary/add " method="post" name="myForm">      

                <fieldset>
                    <legend>Select A Teacher</legend>
                    <dl class="inline">


                        <dt><label for="teachername">Teacher Name <span class="hello">*</span></label></dt>
                        <dd>
                            <?php echo form_dropdown('teacherid', $dropdown_teacher,'', 'class="medium required" id="teacherid"' ); ?>



                        </dd>

                        <dt><label for="salary_month">Month of Salary <span class="hello">*</span></label></dt>
                        <dd>
                        <?php   $month_options = array(     
                                                         ''  => 'Select',   
                                                         'January'  => 'January',
                                                         'February'    => 'February',
                                                         'March'   => 'March',
                                                         'April'    => 'April',
                                                         'May'   => 'May',
                                                         'June'  => 'June',
                                                         'July'    => 'July',
                                                         'August'   => 'August',
                                                         'September'    => 'September',
                                                         'October'   => 'October',
                                                         'November'   => 'November',
                                                         'December' => 'December',
                                                        );


                                        echo form_dropdown('salary_month', $month_options,'', 'class="medium required"'  );?>

                        </dd>

                        <dt><label for="salary_year"> Year of Salary <span class="hello">*</span></label></dt>
                        <dd>



                        <?php 
                            $year_options = array(      
                                                         ''  => 'Select',   
                                                         '2010'  => '2010',
                                                        '2011'  => '2011',
                                                        '2012'  => '2012',
                                                        '2013'  => '2013',
                                                        '2014'  => '2014',

                                                         '2015'    => '2015',
                                                         '2016'   => '2016',
                                                         '2017'    => '2017',
                                                         '2018'   => '2018',
                                                         '2019'  => '2019',
                                                         '2020'    => '2020',
                                                         '2021'   => '2021',
                                                         '2022'    => '2022',
                                                         '2023'   => '2023',
                                                         '2024'   => '2024',
                                                         '2025' => '2025',
                                                        '2026' => '2026',
                                                        '2027' => '2027',
                                                        '2028' => '2028',
                                                        '2029' => '2029',
                                                        '2030' => '2030',
                                                        );


                                        echo form_dropdown('salary_year', $year_options,'', 'class="medium required"'  );?>

                        </dd>

                        <dt><label for="salary_disbursement_date">Salary Disbursement Date <span class="hello">*</span></label></dt>
                        <dd>
                            <input type="text" name="salary_disbursement_date" id="datepicker2" maxlength="10" class="medium required" />

                        </dd>



                        <dt><label for="salaryamount">Salary Amount <span class="hello">*</span></label></dt>
                        <dd>
                            <input type="text" name="salary_amount" id="salary_amount" value="" class="medium required number" />

                        </dd>




                    </dl>

                <div class="buttons">
                        <button id="submit_item" name="submit_item" type="submit" class="button">Submit Button</button>

                    </div>

                </fieldset>
            </form>

I am using jquery validation plugin 1.9.0 . I have downloaded the plugin from here: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

I have copied the following code from this link http://docs.jquery.com/Plugins/Validation/Validator/numberOfInvalids
to find out the number of errors I have in my form. But it is not working.

$(document).ready(function() {
    $('a.delete').click(function() {
        invalidHandler: function() {
            $("#summary").text(validator.numberOfInvalids() + " field(s) are invalid");
        }
    });
});     

And then

<div id="summary" class="summary"> </div>
<a href="#" class="delete">click</a>

My form head looks like this:

<form class="uniform"  autocomplete="off" id="myForm" enctype="multipart/form-data" action="<?echo base_url();?>addteacher_salary/add " method="post" name="myForm">

I don't call any validation function in my form, I just load the validation plugin inside head tag and the validation works.

Could you please tell me what is wrong with the code?

Edit

My Complete form looks like following:

   <form class="uniform"  autocomplete="off" id="myForm" enctype="multipart/form-data" action="<?echobase_url();?>addteacher_salary/add " method="post" name="myForm">      

                <fieldset>
                    <legend>Select A Teacher</legend>
                    <dl class="inline">


                        <dt><label for="teachername">Teacher Name <span class="hello">*</span></label></dt>
                        <dd>
                            <?php echo form_dropdown('teacherid', $dropdown_teacher,'', 'class="medium required" id="teacherid"' ); ?>



                        </dd>

                        <dt><label for="salary_month">Month of Salary <span class="hello">*</span></label></dt>
                        <dd>
                        <?php   $month_options = array(     
                                                         ''  => 'Select',   
                                                         'January'  => 'January',
                                                         'February'    => 'February',
                                                         'March'   => 'March',
                                                         'April'    => 'April',
                                                         'May'   => 'May',
                                                         'June'  => 'June',
                                                         'July'    => 'July',
                                                         'August'   => 'August',
                                                         'September'    => 'September',
                                                         'October'   => 'October',
                                                         'November'   => 'November',
                                                         'December' => 'December',
                                                        );


                                        echo form_dropdown('salary_month', $month_options,'', 'class="medium required"'  );?>

                        </dd>

                        <dt><label for="salary_year"> Year of Salary <span class="hello">*</span></label></dt>
                        <dd>



                        <?php 
                            $year_options = array(      
                                                         ''  => 'Select',   
                                                         '2010'  => '2010',
                                                        '2011'  => '2011',
                                                        '2012'  => '2012',
                                                        '2013'  => '2013',
                                                        '2014'  => '2014',

                                                         '2015'    => '2015',
                                                         '2016'   => '2016',
                                                         '2017'    => '2017',
                                                         '2018'   => '2018',
                                                         '2019'  => '2019',
                                                         '2020'    => '2020',
                                                         '2021'   => '2021',
                                                         '2022'    => '2022',
                                                         '2023'   => '2023',
                                                         '2024'   => '2024',
                                                         '2025' => '2025',
                                                        '2026' => '2026',
                                                        '2027' => '2027',
                                                        '2028' => '2028',
                                                        '2029' => '2029',
                                                        '2030' => '2030',
                                                        );


                                        echo form_dropdown('salary_year', $year_options,'', 'class="medium required"'  );?>

                        </dd>

                        <dt><label for="salary_disbursement_date">Salary Disbursement Date <span class="hello">*</span></label></dt>
                        <dd>
                            <input type="text" name="salary_disbursement_date" id="datepicker2" maxlength="10" class="medium required" />

                        </dd>



                        <dt><label for="salaryamount">Salary Amount <span class="hello">*</span></label></dt>
                        <dd>
                            <input type="text" name="salary_amount" id="salary_amount" value="" class="medium required number" />

                        </dd>




                    </dl>

                <div class="buttons">
                        <button id="submit_item" name="submit_item" type="submit" class="button">Submit Button</button>

                    </div>

                </fieldset>
            </form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总攻大人 2024-12-29 12:02:49

请务必在调用 numberOfInvalids 之前在表单上调用 valid 调用 numberOfInvalids

$(document).ready(function() {
    $('a.delete').click(function() {
        $("#myForm").valid(); // Make validate calculate the number of invalid fields.
        var numInvalid = $("#myForm").numberOfInvalids();

        $("#summary").text(numInvalid + " field(s) are invalid");
    });
});     

示例: http://jsfiddle.net/UURz2/

Be sure to call valid on the form before calling numberOfInvalids:

$(document).ready(function() {
    $('a.delete').click(function() {
        $("#myForm").valid(); // Make validate calculate the number of invalid fields.
        var numInvalid = $("#myForm").numberOfInvalids();

        $("#summary").text(numInvalid + " field(s) are invalid");
    });
});     

Example: http://jsfiddle.net/UURz2/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文