javascript - 将 jQuery 验证与 Bootbox 结合使用

标签 javascript jquery html validation bootbox

我正在尝试让 jQuery 验证插件与 bootbox.js(模态)一起使用。

我在模式中有一个表单,其中包含需要验证的字段。

我在这里创建了我的设置的 jsFiddle:http://jsfiddle.net/rDE2q/

当您单击模式中的“创建 session ”按钮时,它应该验证表单,但事实并非如此。

这是我的代码:

$("[name=newSession]").click(function() {
        bootbox.dialog({
            title: '<i class="fa fa-cog"/></i>&nbsp;&nbsp;New Session',
            buttons: {
                close: {
                    label: '<i class="fa fa-times"></i>&nbsp;&nbsp;Close',
                    className: "btn-danger",
                    callback: function() {}
                },
                doAdd: {
                    label: '<i class="fa fa-check"></i>&nbsp;&nbsp;Create Session',
                    className: "btn-primary",
                    callback: function() {

                            //Validation
                            $('[name=newSessionForm]').validate({
                                debug: true,
                                rules: {
                                    sessionName: {
                                        required: true,
                                    },
                                    startDate: {
                                        required: true,
                                    }
                                },
                                messages: {
                                    sessionName: "Please enter a session name"
                                },
                                highlight: function(a) {
                                    $(a).closest(".form-group").addClass("has-error");
                                },
                                unhighlight: function(a) {
                                    $(a).closest(".form-group").removeClass("has-error");
                                },
                                errorElement: "span",
                                errorClass: "help-block",
                                errorPlacement: function(a, b) {
                                    if (b.parent(".input-group").length) a.insertAfter(b.parent()); else a.insertAfter(b);
                                },
                                submitHandler: function() {}
                            });



                        return false;
                    }
                }
            },
            message: '<form role="form" name="newSessionForm" id="newSessionForm">          <div class="form-group">    <label for="sessionName" class="text-primary">Session Name</label>    <input type="text" class="form-control" id="sessionName" name="sessionName" placeholder="Session Name" required>  </div>  <div class="form-group">    <label for="startDate" class="text-primary">Start Date</label>    <input type="text" class="form-control date" id="startDate" placeholder="Start Date">  </div>  <div class="form-group">    <label for="endDate" class="text-primary">End Date</label>    <input type="text" class="form-control date" id="endDate" placeholder="End Date">  </div></form>'
        });
    });

最佳答案

使用.validate({...}).form()。更新了你的jsfiddle:http://jsfiddle.net/rDE2q/94/

关于javascript - 将 jQuery 验证与 Bootbox 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24637740/

相关文章:

javascript - jQuery.eCalendar() - 对事件使用 for 循环

jquery - 在父级中选择多个类

javascript - LocalStorage noUiSlider 值 ||重要的! : (

javascript - Chrome 扩展程序 : Extension button appears in extensions area but JS doesn't execute on click?

javascript - 使用 jQuery HTML 更新表格行

javascript - 避免克隆 HTMLTemplate 来检索 innerHTML?

javascript - AmCharts - 折线图溢出图形容器

javascript - JavaScript 中的 XML

Javascript 当前时间函数工作

javascript - 在事件处理程序中异步执行函数是否有意义?