bootstrap模态框我点击打开后不提交,这样重复2遍,然后填入数据提交的时候就会一下子重复提交3次!怎么避免呢?或者换种说法,怎么设置在模态框退出的时候就直接销毁,不论什么形式退出(出BUG的是在不点击下方的取消按钮的时候)。下面是代码——
html
<div class="modal fade" id="addModal" role="dialog" aria-labelledby="addModalLabel">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">×</span></button>
<h4 class="modal-title" id="addModalLabel">添加信息</h4>
</div>
<div class="modal-body">
<form class="form" id="addForm">
<div class="form-group">
<input type="text" class="form-control" name="student_id" id="add_studentId" placeholder="学号">
</div>
<div class="form-group">
<input type="text" class="form-control" name="student_name" id="add_studentName"
placeholder="姓名">
</div>
<div class="form-group">
<select class="form-control" name="student_sex" id="add_studentSex">
<option value="男">男</option>
<option value="女">女</option>
</select>
</div>
<div class="form-group">
<select class="form-control" name="department_name" id="add_departmentName"></select>
</div>
<div class="form-group">
<select class="form-control" name="major_name" id="add_majorName"></select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="class_id" id="add_classId" placeholder="班级">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="submitAdd">提交</button>
<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
</div>
</div>
</div>
</div>
主要关注这里的取消按钮,上面有属性data-dismiss
,请问怎么用JS实现?
js
$add.click(function () {
getDepartmentName('#add_departmentName');
getMajorName('#add_majorName');
$add_modal.modal('show');
$('#submitAdd').click(function () {
var json = {
student_id: $('#add_studentId').val(),
student_name: $('#add_studentName').val(),
student_sex: $('#add_studentSex').find('option:selected').val(),
department_id: $('#add_departmentName').find('option:selected').val(),
major_id: $('#add_majorName').find('option:selected').val(),
class_id: $('#add_classId').val()
};
$.ajax({
url: 'addStudent',
method: 'post',
data: json,
success: function (response) {
if (response == '109') {
$add_modal.modal('hide');
$('#addForm')[0].reset();
toastr.success('添加数据成功');
$table.bootstrapTable('refresh');
} else if (response == '101') {
$add_modal.modal('hide');
toastr.warning('添加失败:学号');
} else if (response == '102') {
$add_modal.modal('hide');
toastr.warning('添加失败:姓名');
} else if (response == '103') {
$add_modal.modal('hide');
toastr.warning('添加失败:性别');
} else if (response == '104') {
$add_modal.modal('hide');
toastr.warning('添加失败:所属学院');
} else if (response == '105') {
$add_modal.modal('hide');
toastr.warning('添加失败:所属专业');
} else if (response == '106') {
$add_modal.modal('hide');
toastr.warning('添加失败:班级号');
} else if (response == '107') {
toastr.error('添加失败:数据表中已有相同数据');
bootbox.confirm({
title: '操作提示',
message: '想要替换数据库中已有的信息吗?',
buttons: {
confirm: {
label: '是',
className: 'btn-success'
},
cancel: {
label: '否',
className: 'btn-default'
}
},
callback: function (result) {
if (result) {
$.ajax({
url: 'updateStudent',
method: 'post',
data: json,
success: function (response) {
console.log(response);
if (response == '122') {
$add_modal.modal('hide');
$('#addForm')[0].reset();
toastr.success('更新数据成功');
$table.bootstrapTable('refresh');
} else {
$add_modal('hide');
toastr.error('更新数据失败');
$table.bootstrapTable('refresh');
}
}
});
}
}
});
}
}
});
return false;
});
return false;
});
问题截图
这里可以看到右侧提示了4次(我退出了3次,最后一次一起提交)
控制台请求了4次ajax……
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…