2015-05-19 56 views
0

我是laravel的新用戶。在laravel 4.2中,我有傳遞數據給服務器的問題。我沒有使用表單提交,我使用JavaScript來指代形式的行動,例如下面的代碼:如何在不使用form提交laravel 4的情況下將數據傳遞給服務器?

$(document).ready(function(){ 
    $(".delete_action").click(function(event){ 
     $("#deletecategory").prop('href','/admin/category/'+ event.target.id +'/delete'); 
    }); 
}); 

和我刪除這樣的模式:

× 您確定要刪除此類別嗎? 是 沒有

當我點擊是的,它不會做任何事情。我希望能得到你的一些解決方案!

+0

嘗試過使用ajax嗎? –

回答

0

您需要包括一個Ajax調用...實際提交數據......

是這樣的...

$(document).ready(function(){ 
    $(".delete_action").click(function(event){ 
     // incase the button is inside a form, this will prevent it from submitting 
     event.preventDefault(); 
     // get your url 
     var url = '/admin/category/'+ event.target.id +'/delete'; 
     // Create alert to confirm deletion 
     var conf = confirm("Are you sure you want to Delete this?"); 
     if(conf){ 
     // If they click yes 
     // submit via ajax 
      $.ajax({ 
      url:url, 
      dataType:'json', 
      success:function(data){ 
      //put anything you want to do here after success 
      // Probably remove the element from the page since you deleted it    //So if the button is part of a parent div that needs to be removed. 
      } 
     }); 
     } 
    }); 
    }); 

你也可以使用$不用彷徨,而不是$就到縮短代碼更多...

$.get(url, function(data){ 
     //remove element after success 
    }); 

但我意識到你試圖將URL傳遞給模態窗口,然後提交該模態窗口。所以你需要將ajax調用附加到模式窗口按鈕。不像上面那樣,只是打開一個警報窗口。它更簡單的方法,但看起來不那麼花哨。如果你真的想要一個模式。您需要將上述代碼附加到模態確認按鈕。但要點是一樣的。

相關問題