简体   繁体   English

使用jquery验证插件在页面上验证和提交多个表单

[英]Validate and Submit Multiple forms on a page using jquery validation plugin

I am facing trouble getting the multiple forms on a webpage submitted using the jquery validation plugin 我在使用jquery验证插件提交的网页上获取多种表单时遇到了麻烦

Below is the code for generating multiple forms on a page 下面是在页面上生成多种表单的代码

<?php for($index=1;$index<4;$index++) : ?>
            <tr>
                <td class="center">
                <div class="box-content" id="share_rss_">
                    <form class="share_rss_form" id="share_rss_form_<?php echo $index; ?>" method="post" action="abc2_db.php?id=<?php echo $index; ?>" data-noty-options='{"text":"This is a success notification","layout":"topLeft","type":"success"}'>
                        <table class="table table-bordered table-striped table-condensed " id="tbl_<?php echo $index; ?>" >


                            <tr  id='tr_<?php echo $index; ?>'><td><input type=text class="required" name='rss_title' id='title' style="width:400px;margin:0px" value="This is just a demo"></td>
                            </tr>

                            <tr  id='tr_<?php echo $index; ?>'><td><textarea class="required" name='rss_description_<?php echo $index; ?>' style="width:400px;margin:0px" >Description series</textarea></td></tr>
                            <tr  id='tr_<?php echo $index; ?>'><td><b>Featured</b>&nbsp;&nbsp;<input type='checkbox' name='rss_featuredArticle_<?php echo $index; ?>' value=1 />
                                &nbsp;&nbsp;&nbsp;&nbsp;<b>Rated</b>&nbsp;&nbsp;<input type='checkbox' name='rss_rated_<?php echo $index; ?>' value=1 />
                                &nbsp;&nbsp;&nbsp;&nbsp;<b>Expiry time</b>&nbsp;&nbsp;<input type='checkbox' name='rss_expirySelect_<?php echo $index; ?>' value=1 />
                                <select name='rss_expiry_<?php echo $index; ?>'><option value=15>15</option><option value=30>30</option></select></td>
                            </tr>
                            <tr  id='tr_<?php echo $index; ?>'>
                            <td><select class="category" class="required" name="category_<?php echo $index; ?>" id="category_<?php echo $index; ?>">
                                <option value=""    ><b>---Select---</b></option>
                                <?php foreach($category as $c) :?>
                                <option value="<?php echo $c; ?>" ><b><?php echo $c; ?></b></option>
                                <?php endforeach; ?>
                                <br>
                                </select>
                            </td>
                            </tr>
                            <tr  id='tr_<?php echo $index; ?>'><td><div id="container_for_category_<?php echo $index; ?>"></div></td>
                            </tr>

                        </table>
                    </form>
                </div>
                </td>
                <td class="center">
                    <button class="btn btn-info approveid" href="#" type="submit" id="approve_<?php echo $index; ?>"><i class="icon-edit icon-white"></i></button>
                    <button class="btn btn-danger trashid" href="#" type="submit" id="trash_<?php echo $index; ?>"><i class="icon-trash icon-white"></i></button>
                </td>
            </tr>
            <?php endfor; ?>

Below is the jquery code to trigger the validation and submission of forms 以下是触发表单验证和提交的jQuery代码

    $('.share_rss_form').each(function(index,e1){
        $(e1).validate({
            errorClass: "error",
            validClass: "success",
            submitHandler: function(form) { 
                                    //The below alert is triggered
                alert(form.id)      ;
                form.submit();
                return true;
                //alert(e1);
                //console.log(form);

            }
        });

    });

The problem is the above jquery seems to work partially because alert(form.id) in the validation submitHandler code. 问题是上面的jquery似乎部分起作用,因为验证SubmitHandler代码中的alert(form.id)。 However the form doesn't submit. 但是,该表单未提交。 I have added form.submit() in the code after which the form does submit but the entire page is reloaded. 我在代码中添加了form.submit(),之后表单确实提交了,但是整个页面都被重新加载了。

According to me the jquery validation plugin should automatically submit the form after validation but it doesnt seem so. 据我说,jQuery验证插件应在验证后自动提交表单,但事实并非如此。 Can someone please help? 有人可以帮忙吗?

Thanks in advance! 提前致谢!

My guess is when you use .each(), it tries to validate the elements one by one. 我的猜测是,当您使用.each()时,它将尝试逐一验证元素。 Then the validator submits the invalid form and reloads the page in order to validate next form element. 然后,验证器提交无效的表单并重新加载页面,以验证下一个表单元素。 Basically calling validate again and again. 基本上一次又一次地验证。

The answer to above was the use $(form).ajaxSubmit(); 上面的答案是使用$(form).ajaxSubmit();。 instead of simply form.submit(). 而不是简单的form.submit()。

    $('.share_rss_form').each(function(index,e1){
        $(e1).validate({
            errorClass: "error",
            validClass: "success",
            submitHandler: function(form) { 
                                    //The below alert is triggered
                alert(form.id);
                $(form).ajaxSubmit();

            }
        });

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多个jQuery验证引导插件不会验证多种形式 - multiple jQuery validation bootstrap plugin do not validate multiple forms 当单个页面上存在 2 个表单时,如何使用 jQuery 表单验证插件验证 reCaptcha? - How to validate reCaptcha using jQuery Form Validation plugin when 2 forms are present on a single page? Jquery在一个页面上验证并提交更多表单 - Jquery validate and submit more forms on one page jQuery表单在同一页面上验证并提交多个表单会给出“未捕获的SyntaxError:无效或意外的令牌” - jQuery form validate and submit multiple forms on the the same page gives “Uncaught SyntaxError: Invalid or unexpected token” jQuery validate bootstrap插件不验证多个表单 - jQuery validate bootstrap plugin does not validate multiple forms Jquery.validate - 一页 - 多个表单 - Jquery.validate - one Page - multiple forms 如何使用jQuery验证插件验证字段 - how validate fields using jquery validation plugin 使用jQuery验证插件验证后使用ajax提交表单提交? - form submit using ajax after validation using jquery validation plugin? 使用jquery验证插件进行多案例验证 - Multiple case validation using jquery validation plugin jQuery,使用.each()以多种形式提交() - jQuery, submit() with multiple forms using .each()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM