weixin_33724570 2015-02-09 17:56 采纳率: 0%
浏览 39

阿贾克斯不起作用

I have a problem with ajax method. I have a form with two fields, email and password. When I use the label "a" with id = registroAction, I call the ajax method and jump somewhere else.

My code:

<form id="form-registro" class="margen-left-445 absoluto margen-top-40 azul-corporativo">
        <p class="bg-success espaciointerior-10 mensaje_activacion" style="display:none"></p>
        <p class="bg-danger espaciointerior-10 mensaje_activacion" style="display:none"></p>
        <h3 style="text-align: center; color: #044E7C;">Registrate gratis</h3>
        <div class="input-group">
            <input type="text" id="email" class="input-generico margen-top-10 margen-bottom-5" placeholder="Email" aria-describedby="basic-addon2">
        </div>
        <div class="input-group">
            <input type="password" id="password" class="input-generico margen-top-10 margen-bottom-5" placeholder="Contraseña" aria-describedby="basic-addon2">           
        </div>
        <div class="input-group margen-bottom-20">
            <input type="password" class="input-generico margen-top-10 margen-bottom-5" placeholder="Repita contraseña" aria-describedby="basic-addon2">
        </div>
        <a id="registroAction" class="btn boton-generico margen-left-38">Crear una cuenta gratis</a>
        <a href="<?php echo base_url(); ?>" class="btn boton-generico">Cancelar</a>
    </form>

Jquery:

<script type="text/javascript">
$(function() {
    $("#registroAction").on("click", function(e){
        $.ajax({            
            url: '<?php echo base_url(); ?>frontend/registro/registrarAction',
            type: 'POST',
            dataType: 'json',  
            data: {email: $('#email').val(), password: $('#password').val()},
            success: function (response) {                   
                if ( response.ok == 'ok' ){
                    // Establecemos la clase necesario para mostrar el mensaje
                    var classname = ( response.ok == 'ok' ) ? '.bg-success' : '.bg-danger';
                    // Ocultamos el resto de mensajes de activacion
                    $('.mensaje_activacion').hide().html('');
                    // Mostramos y rellenamos según si ha salido bien o mal
                    $('.mensaje_activacion' + classname).html(response.responseText).show().fadeOut( 3000 );

                } else {
                    $(location).attr('href',response.url);
                }
            },
            error: function(estado){
                console.log(estado)
            }
        });
    });
});

  • 写回答

2条回答 默认 最新

  • derek5. 2015-02-09 18:00
    关注

    First, your tag should have an href attribute. You can use href="#". Regarding your issue, you need to tell the browser to ignore the default behavior (following links):

    $("#registroAction").on("click", function(e) {
      e.preventDefault();
      ....
    }
    

    This should work and prevent the weird behavior you are seeing!

    评论

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算