donk68254 2016-07-11 17:46
浏览 30

复制ajax以调用模态并关闭

I have a subscription form which works through modal windows, but when I close the window and return to click the button to subscribe ajax calls are duplicated, which is an error.

            $("#openModal").click(function() {
            if($("#wname").val() == '' || $("#lname").val() == '' || $("#wmail").val() == ''){
                $('#message-error').html("Los campos no pueden estar vacios");
                return;
            }
            $("#message").empty();
            var regex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
            if (regex.test($('#wmail').val())) {
                $('#myModal').modal('show');
                $('#fin_coach_acept').click(function(event){
                    $('.whmessage').empty();
                    $('#message').empty();
                    datos['usu_nombre'] = $("#wname").val();
                    datos['usu_apellido'] = $("#lname").val();
                    datos['pa_id'] = 32;
                    datos['usu_fechanacimiento'] = '';
                    datos['sexo'] = $("#sexo_e").val();
                    datos['firma'] = '';
                    datos['mail'] = $("#wmail").val();
                    datos['intereses'] = '';
                    datos['rol_id'] = 2;
                    datos['pa_residencia'] = '';
                    datos['curso'] = $(".whcursos").val();
                    datos['contmin'] = $("#contmin").val();
                    datos['contper'] = $("#contper").val();
                    console.log(JSON.stringify(datos));
                    $.ajax({
                        type: "POST",
                        url: "<?php echo home_url(); ?>/wssetcoach.php",
                        data: {datosw: JSON.stringify(datos)},
                        success: function(res){
                            if(res == 'logrado'){
                                $("#message").append("<span style='color:#76292F;'>Usted ya se encuentra registrado como Facilitador con: "+ $("#wmail").val() + "</span>");
                            } else {

                                $('.whmessage').append("<p>¡Gracias por registrarte!. Desde ahora eres parte de ActiveMind. Se enviarán los datos de acceso al Sistema Académico a la cuenta de correo:</p><p>"+$("#wmail").val()+"</p><p>Cualquier consulta envía un correo electrónico a la dirección:</p><p>am@activemind.center</p>");
                                $('#final').modal('show');
                                $('#finalize').click(function(event){
                                    $(location).attr('href', 'http://www.activemind.center/active/');
                                });
                                $('#myModal').modal('hide');
                                $('#final').modal('show');
                                $('#coach_cancel').click(function(event){
                                    $('#facilitadorini').modal('hide');
                                    $('#myModal').modal('show');
                                });
                            }
                        }
                    });
                });
            } else {
                $('#message-error').html("El correo no es válido");
                return;
            }
        });

Apparently, manners is kept in memory, it has been used the following code to delete it but does not work: $('#studentini').remove();

Any idea solution will be useful to me, thanks

  • 写回答

1条回答 默认 最新

  • dqsh30374 2016-07-11 17:49
    关注

    Look into using jquery's off() method to avoid your click handler being added again each time you open the modal.

    var $openBtn = $("#openModal");
    
    $openBtn.off().click(fn);
    

    Or use event delegation to avoid this issue:

    var $document = $(document);
    
    document.on('click', '#openModal', fn);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c