是的,這將用ajax解決。
下面是當頁面應該刷新
$('#button').click(function() {
$.ajax({
url: 'path/to/script.php',
type: 'post',
dataType: 'html', // depends on what you want to return, json, xml, html?
// we'll say html for this example
data: formData, // if you are passing data to your php script, needed with a post request
success: function(data, textStatus, jqXHR) {
console.log(data); // the console will tell use if we're returning data
$('#update-menu').html(data); // update the element with the returned data
},
error: function(textStatus, errorThrown, jqXHR) {
console.log(errorThrown); // the console will tell us if there are any problems
}
}); //end ajax
return false; // prevent default button behavior
}); // end click
jQuery的阿賈克斯
http://api.jquery.com/jQuery.ajax/
腳本解釋的代碼示例。
1 - 用戶單擊按鈕。
2 - 點擊功能啓動一個XHR呼叫到服務器。
3 - url是一個php腳本,它將根據發佈的值處理我們發送的數據。
4 - 該類型是一個POST請求,它需要數據返回數據。
5 - 在這種情況下的dataType將是html。
6 - 我們發送給腳本的數據可能是分配給變量formData的表單元素的序列化。
7 - 如果XHR返回200,則在控制檯中登錄返回的數據,以便我們知道我們正在處理什麼。然後將數據作爲html放入選定元素(#update-menu)中。
8 - 如果出現錯誤,控制檯會爲我們記錄錯誤。
9 - 返回false以防止默認行爲。
10 - 全部完成。