2012-02-24 34 views
61

我需要一個簡單的ajax教程或案例研究一個簡單的輸入表單,我想通過輸入表單發送一個用戶名,它將它發送到數據庫並回復結果。
任何建議這樣的教程是受歡迎的,因爲我只有一個使用Mootool,但我正在尋找一個使用jQuery!Ajax教程發佈,並得到

+0

http://blog.teamtreehouse.com/beginners-guide-to- ajax-development-with-php – ytpillai 2016-04-08 13:07:46

+0

是的好問題... – 2018-01-01 06:15:40

回答

105

你可以試試這個:

$.ajax({ 
    url: "test.html", 
    cache: false, 
    success: function(html){ 
    $("#results").append(html); 
    } 
}); 

此代碼將test.html文件的內容附加到#results元素

您可以在jQuery website找到更多的信息。

更新:

使用此代碼來發送POST數據和輸出結果。

var menuId = $("ul.nav").first().attr("id"); 
var request = $.ajax({ 
    url: "script.php", 
    type: "POST", 
    data: {id : menuId}, 
    dataType: "html" 
}); 

request.done(function(msg) { 
    $("#log").html(msg); 
}); 

request.fail(function(jqXHR, textStatus) { 
    alert("Request failed: " + textStatus); 
}); 
23

假設你有一些HTML代碼,如:

<input type="text" name="username" id="username"> 
<div id="resultarea"></div> 

你會使用一個<script>,如:

var myusername = $("#username").val(); 
$.ajax({ 
    type: "GET", 
    url: "serverscript.xxx", 
    data: myusername, 
    cache: false, 
    success: function(data){ 
    $("#resultarea").text(data); 
    } 
}); 
+1

您可能需要設置參數名稱,如數據:「username =」+ myusername – GrandMasterFlush 2016-06-27 11:43:59