【问题标题】:Jquery break Href clickJquery打破Href点击
【发布时间】:2010-12-30 18:57:24
【问题描述】:

我有一个带有 Jquery 的 MVC 应用程序;我有一个带有一些数据的网格视图和一个指向控制器新操作的元素。 现在我需要在执行操作之前进行一些验证。 我的代码是这样的:

$('#datosCartolaSeguro a').click(function(e) {

    var datosProductoSeleccionado = $(this).parent().find('input').val();
    $.post(pathSite + 'PorSegurosCartola/ProductoTieneCartola',
                {
                    strDatosProducto: datosProductoSeleccionado
                },
                function(resp) {
                    if (resp == 'False') {
                        popupActual = '#popupProdNoTieneCartola';
                        centrarPopup();
                        cargarPopup();

                    }
                    else {
                        mostrarVentanaAdvertencia();
                        return true;
                    }
                }
            );
});

调用方法后:cargarPopup() 想避免action链接点击的执行。我尝试了一个简单的 return false,但该页面进行了回发。我还尝试了函数 e.preventDefault();已在此论坛的其他解决方案中发布,但它不起作用。我需要避免点击 ActionLink 但没有回发。 谢谢。

【问题讨论】:

    标签: jquery


    【解决方案1】:

    在点击函数末尾添加return false:

    $('#datosCartolaSeguro a').click(function(e) {
    
        var datosProductoSeleccionado = $(this).parent().find('input').val();
        $.post(pathSite + 'PorSegurosCartola/ProductoTieneCartola',
                    {
                        strDatosProducto: datosProductoSeleccionado
                    },
                    function(resp) {
                        if (resp == 'False') {
                            popupActual = '#popupProdNoTieneCartola';
                            centrarPopup();
                            cargarPopup();
    
                        }
                        else {
                            mostrarVentanaAdvertencia();
                            return true;
                        }
                    }
                );
    return false;
    });
    

    【讨论】:

      【解决方案2】:

      在等待对帖子的响应时,您需要阻止事件的发生,因此您应该添加 return false;或发布后调用后的 e.preventDefault()。

      $('#datosCartolaSeguro a').click(function(e) 
      {
      
       var datosProductoSeleccionado = $(this).parent().find('input').val();
       $.post(pathSite + 'PorSegurosCartola/ProductoTieneCartola',
                  {
                      strDatosProducto: datosProductoSeleccionado
                  },
                  function(resp) {
                      if (resp == 'False') {
                          popupActual = '#popupProdNoTieneCartola';
                          centrarPopup();
                          cargarPopup();
      
                      }
                      else {
                          mostrarVentanaAdvertencia();
                          window.location=$('#datosCartolaSeguro a').href;
                      }
                  }
              );
      e.preventDefault();
      };
      

      【讨论】:

      • return false 并没有解决我的问题,现在事件点击永远不会触发(我的意思是,操作链接永远不会重定向到它应该重定向到的页面)我该如何处理它?谢谢
      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多