防止bootbox关闭弹出窗口 [英] Prevent bootbox from closing pop-up window
问题描述
我正在使用bootbox制作带有表单的弹出窗口,如果表单字段有问题,我必须验证它们并向用户抛出错误。
但是在用户点击发送
按钮后,我无法阻止启动箱窗口关闭。我需要向用户显示错误通知,因此可以更正错误并再次发送表单。
I am using bootbox to make pop-up windows with forms and I have to validate them and throw error to user if something is wrong with the form fields.
But I cannot prevent bootbox window from closing after user clicks 'Send
' button. I need to show error notifications to user, so errors could be corrected and the form be sent again.
return false
工作正常,但在它之后我找不到方法,恢复通常的bootbox方法来关闭窗口。
return false
works ok, but after it I cannot find method, to restore usual method of bootbox to close the windows.
是否有人遇到同样的问题以及你如何摆脱这种情况?
Does somebody faced the same problem and how you get rid of this situation?
根据要求, fsFiddle :
<button id="test">Bootbox</button>
代码:
$(document).ready(function() {
$("#test").on('click', function() {
bootbox.dialog({
title: "This is a form in a modal.",
message: '<div class="row"> ' +
'<div class="col-md-12"> ' +
'<form class="form-horizontal"> ' +
'<div class="form-group"> ' +
'<label class="col-md-4 control-label" for="name">Name</label> ' +
'<div class="col-md-4"> ' +
'<input id="name" name="name" type="text" placeholder="Your name" class="form-control input-md"> ' +
'<span class="help-block">Here goes your name</span> </div> ' +
'</div> ' +
'<div class="form-group"> ' +
'<label class="col-md-4 control-label" for="awesomeness">How awesome is this?</label> ' +
'<div class="col-md-4"> <div class="radio"> <label for="awesomeness-0"> ' +
'<input type="radio" name="awesomeness" id="awesomeness-0" value="Really awesome" checked="checked"> ' +
'Really awesome </label> ' +
'</div><div class="radio"> <label for="awesomeness-1"> ' +
'<input type="radio" name="awesomeness" id="awesomeness-1" value="Super awesome"> Super awesome </label> ' +
'</div> ' +
'</div> </div>' +
'</form> </div> </div>',
buttons: {
success: {
label: "Save",
className: "btn-success",
callback: function () {
var name = $('#name').val();
var answer = $("input[name='awesomeness']:checked").val()
console.log(name + " " + answer);
}
}
}
});
});
});
推荐答案
我不是百分之百确定这是什么你要。我理解为:保持模态打开,直到表格有效。
I am not 100% sure about what it is that you want. I understand it as: "Keep the modal open until the form is valid".
如果这是你需要的,你可以这样做:
If this is what you need, you could proceed as such:
callback: function () {
var name = $('#name').val();
var answer = $("input[name='awesomeness']:checked").val()
console.log(name + " " + answer);
// proceed to your validation, if your form is not valid
// the validation should return false
var formIsValid = doFormValidation();
if(!formIsValid) {
// show error messages to the user here
showFormErrors();
// prevent the modal from closing
return false;
}
}
这篇关于防止bootbox关闭弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!