简体   繁体   English

重置JQuery表单验证

[英]Reset JQuery Form Validation

I have used Jquery form validation on Category Form Fill-up which is pop-up by modal. 我已经在类别表单填充中使用了Jquery表单验证,该表单表单是通过模式弹出的。 When user make form blank and Hit Save; 当用户将表格设为空白并点击保存时; JQuery validation error is fired. 触发jQuery验证错误。

Now if user close this modal(Not page refresh) and open that modal again those errors are already there. 现在,如果用户关闭此模式(不刷新页面)并再次打开该模式,则这些错误已经存在。 i needs it to be cleared. 我需要将其清除。 在此处输入图片说明

<div id="addcategory"  class="modal fade">
                    <div class="modal-dialog new-modal-dialog">
                        <div class="modal-content">
                            <!-- dialog body -->
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                                <h4 class="modal-title">Add New Category</h4>
                            </div>
                            <?php echo form_open(base_url('admin/business/addNewCat'), array('class' => 'form_add_new_cat')); ?>
                            <div class="modal-body alert-message">
                                <div class="row">
                                    <div class="col-md-3">
                                        Category Name:
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                        <input type="text" name='add_new_catname' 
                                                                 class="add_new_catname form-control" 
                                                                 placeholder="Enter New Category Name" value="" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- dialog buttons -->
                            <div class="modal-footer"> 
                                <button type="submit" class="btn btn-success btn_add_new_cat">Save</button>
                                <button type="button" class="btn btn-default no" data-dismiss="modal" >Cancel</button>
                            </div>  
                            <?php echo form_close(); ?>
                        </div>
                    </div>
                </div>

How to do so? 怎么做?

There is a method built in that allows you to cancel the validation errors. 有一个内置的方法,允许您取消验证错误。 For your example, the code would look similar to: 对于您的示例,代码看起来类似于:

$form = $('.form_add_new_cat');
$validator = $form.validate(); //init validator

$validator.resetForm();
$form.find('.control-group').removeClass('.error'); //bootstrap 3 support

Reference here 这里参考

我已经通过这种方式解决了:

<button type="button" class="btn btn-default no" data-dismiss="modal" onclick="$('.has-error').children('p').remove('p'); $('.has-error').removeClass('has-error')">Cancel</button>

use this --

http://jsfiddle.net/5LCSU/

it will remove all text and also validation it will work without refreshing page.. 它将删除所有文本,并且还可以进行验证,而无需刷新页面。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM