[英]how to pass data to server without using form submit in laravel 4?
我是laravel的新用戶。 我在將數據傳遞到laravel 4.2中的服務器時遇到問題。 我沒有使用表單提交,而是使用javascript來引用表單操作,例如以下代碼:
$(document).ready(function(){
$(".delete_action").click(function(event){
$("#deletecategory").prop('href','/admin/category/'+ event.target.id +'/delete');
});
});
和我的刪除方式如下:
× 確定要刪除此類別嗎? 是否當我單擊“是”時,它不會執行任何操作。 我希望從您那里得到一些解決方案!
您可以為此使用http://api.jquery.com/jquery.ajax/ 。
$('#yourOkButton').click(function(){$.ajax(...);});
$ .ajax的文檔中記錄了所有內容。
您需要包含一個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而不是$ .ajax來縮短代碼更多...
$.get(url, function(data){
//remove element after success
});
但是我意識到您正在嘗試將URL傳遞到模式窗口,然后提交該模式窗口。 因此,您需要將ajax調用附加到模式窗口按鈕。 不像上面那樣,它只是打開一個警報窗口。 它是更簡單的方法,但是看起來不太花哨。 如果您真的想要一個模態。 您需要將上面的代碼附加到模式確認按鈕。 但是要旨是一樣的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.