如果有人在文本框中输入了错误的文字,我想阻止表单提交。我想用户只输入完成然后提交表单,否则会显示错误消息。在我的下面的代码不工作,它不会阻止提交表单。
$('#delete_btn').click(function(event) {
event.preventDefault();
bootbox.prompt("Are you sure you want to done the record?<br/>Write 'done' below to confirm!", function(result) {
if(result !== null)
result = result.toLowerCase();
if(result === 'done') {
} else {
alert('please enter something in textbox');
}
});
});
答案 0 :(得分:2)
如果您希望对话框保持打开状态,请在回调中添加return false
,ergo:
$('#delete_btn').click(function(event) {
event.preventDefault();
bootbox.prompt("Are you sure you want to done the record?<br/>Write 'done' below to confirm!", function(result) {
if(result !== null){
result = result.toLowerCase();
if(result === 'done') {
/* submit your form */
} else {
alert('please enter something in textbox');
return false;
}
}
});
});
您也可以始终返回false,然后仅在成功状态下手动关闭提示:
$('#delete_btn').click(function(event) {
event.preventDefault();
bootbox.prompt("Are you sure you want to done the record?<br/>Write 'done' below to confirm!", function(result) {
if(result !== null){
result = result.toLowerCase();
if(result === 'done') {
/* submit your form, then... */
bootbox.hideAll();
// or $('.bootbox').modal('hide');
} else {
alert('please enter something in textbox');
}
} else {
bootbox.hideAll();
}
});
return false;
});