2009-12-29 102 views
2

我有一个MVC应用程序与Jquery;我有一个gridview有一些数据和一个指向控制器新动作的元素。 现在我需要在执行操作之前进行一些验证。 我的代码是这样的:jquery break Href点击

$('#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()我想避免操作链接的点击执行。我尝试了一个简单的返回false,但页面做了回发。我也尝试过使用函数e.preventDefault();这是张贴在这个论坛上的其他解决方案,但它没有奏效。我需要避免点击ActionLink但没有回传。 谢谢。

回答

1

您需要甚至阻止发生了,而你正在等待响应请求,所以你应该将返回FALSE;或者在post调用之后调用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(); 
}; 
+0

返回false并不解决我的问题,现在的情况下点击从未触发(我的意思是,动作链接从未重定向到该页面它应该)我如何处理呢? Thanxs – lidermin

+0

我更新了答案。 –

3

添加在点击函数的最后返回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; 
});