0

我正在使用这个jQuery 插件进行表单验证。

我在 BootStrap 模式上有 2 个表单,如下所示。

  <div class="modal fade" id="editcourse{{ $course->id }}" tabindex="-1" role="dialog"
        aria-labelledby="exampleModalCenterTitle" aria-hidden="true">                                      
        <form action="{{ route('admin.subCourse.update') }}" method="post" id="update_course">
            @csrf
            <div class="modal-body">
                <div class="form-group">
                    <div class="col-md-10">
                        <label>Total Questions</label>
                        <input type="text" class="form-control" name="total_questions">
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary"
                    data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary">Update</button>
            </div>
        </form>                                        
    </div>


    <div class="modal fade" id="createnewcourses" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle"
        aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">                
                <form action="{{ route('admin.subCourse.save') }}" method="post" id="create_course">
                    @csrf
                    <div class="modal-body">
                        <div class="form-group">
                            <div class="col-md-10">
                                <label>Total Questions</label>
                                <input type="text" class="form-control" name="total_questions">
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">Save</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

我的 jQuery 代码如下所示

$(document).ready(function () {
 // This one is working
    $('#create_course').validate({
        errorClass: "error fail-alert",
        validClass: "valid success-alert",
        rules: {
            total_questions: {
                required: true
            }
        }
    });


    $('#update_course').validate({
     // This one is Not working
        errorClass: "error fail-alert",
        validClass: "valid success-alert",
        rules: {
            total_questions: {
                required: true
            }
        }
    });
});

第一次验证有效,但第二次验证无效。

4

1 回答 1

1

请尝试以下脚本

$(document).ready(function () { // This one is working $('#create_course, #update_course').validate({ errorClass: "error fail-alert", validClass: "valid success-alert", rules: { total_questions: { required: true } } });

于 2021-02-17T10:43:30.043 回答