【问题标题】:Bootbox validation引导箱验证
【发布时间】:2013-08-25 00:41:00
【问题描述】:

我正在尝试使用 Bootbox 创建模式。我有模态弹出窗口并要求您填写一些数据。然后我尝试进行验证,所以当他们点击保存时,它会检查以确保字段已填写。

如果验证失败,如何防止模态框在单击保存时关闭?

bootbox.dialog(header + content, [{
    "label": "Save",
    "class": "btn-primary",
    "callback": function() {

        title = $("#title").val();
        description = $("#description").val();
        icon = $("#icon").val();
        href = $("#link").val();
        newWindow = $("#newWindow").val();
        type = $("#type").val();
        group = $("#group").val();

            if (!title){ $("#titleDiv").attr('class', 'control-group error'); } else {
                addMenu(title, description, icon, href, newWindow, type, group);
            }
    }
}, {
    "label": "Cancel",
    "class": "btn",
    "callback": function() {}
}]);

【问题讨论】:

    标签: javascript jquery bootbox


    【解决方案1】:

    我认为您可以在“保存”按钮回调中返回 false

    像这样:

    bootbox.dialog(header + content, [{
        "label": "Save",
        "class": "btn-primary",
        "callback": function() {
    
            title = $("#title").val();
            description = $("#description").val();
            icon = $("#icon").val();
            href = $("#link").val();
            newWindow = $("#newWindow").val();
            type = $("#type").val();
            group = $("#group").val();
    
                if (!title){ 
                    $("#titleDiv").attr('class', 'control-group error'); 
                    return false; 
                } else {
                    addMenu(title, description, icon, href, newWindow, type, group);
                }
        }
    }, {
        "label": "Cancel",
        "class": "btn",
        "callback": function() {}
    }]);
    

    【讨论】:

    • 但是如果回调返回false,它不会通过单击“取消”按钮关闭模型。请提出您的想法。
    【解决方案2】:

    正如@AjeetMalviya 评论的那样,@bruchowski 发布的解决方案在以这种方式使用return false; 时不会关闭 Bootbox。单击取消按钮时回调返回null,单击确定按钮时返回空字符串。

    <script>
        var bb = bootbox.prompt({
            title: 'Input Required',
            onEscape: true,
            buttons: {
                confirm: {
                    label: '<svg><use xlink:href="/icons.svg#check" /></svg> OK'
                },
                cancel: {
                    label: '<svg><use xlink:href="/icons.svg#x" /></svg> Cancel',
                    className: 'btn-secondary'
                }
            },
            inputType: 'password',
            callback: function (result) {
                //result is null when Cancel is clicked
                //empty when OK is clicked
                if (result === null) {
                    return;
                } else if (result === '') {
                    bb.find('.bootbox-input-password').addClass('input-validation-error');
                    return false;
                }
    
                console.log(result);
            }
        });
    
        bb.init(function () {
            //do stuff with the bootbox on startup here
        });
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      相关资源
      最近更新 更多