javascript - 关于bootstrap模态框重复打开后提交如何避免重复提交?
伊谢尔伦
伊谢尔伦 2017-04-11 12:47:00
[JavaScript讨论组]

bootstrap模态框我点击打开后不提交,这样重复2遍,然后填入数据提交的时候就会一下子重复提交3次!怎么避免呢?或者换种说法,怎么设置在模态框退出的时候就直接销毁,不论什么形式退出(出BUG的是在不点击下方的取消按钮的时候)。下面是代码——

html


            

主要关注这里的取消按钮,上面有属性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……

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(2)
迷茫

观察你的代码发现一个问题。在$add.click()事件里面,点击一次就会添加一次 $('#submitAdd').click(),最后导致执行多个回调也就是发送多个请求。所以把$('#submitAdd').click()移到外面去。
另:如果希望一定要点击取消关闭模态框,可设置

$('#myModal').modal({
  backdrop:'static',
  keyboard: false
})
大家讲道理

$add.click(function () {
            getDepartmentName('#add_departmentName');
            getMajorName('#add_majorName');
            $add_modal.modal('show');
          
            return false;
        });
  $('#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;
            });
            

还有你这坨 if (response == '109')你不累吗。。

你就不能固定格式json

{
    "code":101,
    "message":"添加失败:学号"
}
{
    "code":0,
    "message":"添加数据成功"
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号