2009-12-29 100 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; 
});