Bootbox验证

SBB*_*SBB 2 javascript jquery bootbox

我正在尝试使用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() {}
}]);
Run Code Online (Sandbox Code Playgroud)

bru*_*ski 9

我想你可以在"保存"按钮回调中返回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() {}
}]);
Run Code Online (Sandbox Code Playgroud)