jQuery中get方法用法分析
本文實例講述了jQuery中get方法用法。分享給大家供大家參考,具體如下:
參數(shù):url,[data],[callback],[type]
| url | 待載入頁面的URL地址。 |
| data | 待發(fā)送 Key/value 參數(shù)。 |
| callback | 載入成功時回調函數(shù)。 |
| type | 返回內容格式,xml, html, script, json, text, _default。 |
案例1
表單代碼:
<form id="form1" action="#"> <p>評論:</p> <p>姓名: <input type="text" name="username" id="username" /></p> <p>內容: <textarea name="content" id="content" rows="2" cols="20"></textarea></p> <p><input type="button" id="send" value="提交"/></p> </form>
待處理div代碼:
<div class='comment'>已有評論:</div> <div id="resText" > </div>
jQuery代碼:
<script type="text/javascript">
//<![CDATA[
$(function(){
$("#send").click(function(){
$.get("get1.php", {
username : $("#username").val() , //傳入?yún)?shù)
content : $("#content").val()
}, function (data, textStatus){
$("#resText").html(data); // 把返回的數(shù)據(jù)添加到頁面上
}
);
})
})
//]]>
</script>
PHP代碼:
<?php
header("Content-Type:text/html; charset=utf-8");
echo "<div class='comment'><h6>{$_REQUEST['username']}:</h6><p class='para'>{$_REQUEST['content']}</p></div>";
?>
當用戶點擊send按鈕時,觸發(fā)click事件,對數(shù)據(jù)進行處理。主要傳入兩個參數(shù),一個是用戶名,一個是內容。這兩個參數(shù)被傳遞到php頁面。PHP頁面處理完畢后,返回輸入數(shù)據(jù),get方法處理返回的數(shù)據(jù)。分析代碼,可以看出,這數(shù)據(jù),被寫入了resText這個div層中。整個過程頁面并沒有刷新。很安靜的處理了數(shù)據(jù)的傳輸。
案例2,以xml的格式處理數(shù)據(jù)
表單代碼同上。
待處理div代碼同上。
jQuery代碼:
<script type="text/javascript">
//<![CDATA[
$(function(){
$("#send").click(function(){
$.get("get2.php", {
username : $("#username").val() ,
content : $("#content").val()
}, function (data, textStatus){
var username = $(data).find("comment").attr("username");
var content = $(data).find("comment content").text();
var txtHtml = "<div class='comment'><h6>"+username+":</h6><p class='para'>"+content+"</p></div>";
$("#resText").html(txtHtml); // 把返回的數(shù)據(jù)添加到頁面上
});
})
})
//]]>
</script>
PHP代碼:
<?php
header("Content-Type:text/xml; charset=utf-8");
echo "<?xml version='1.0' encoding='utf-8'?>".
"<comments>".
"<comment username='{$_REQUEST['username'] }' >".
"<content>{$_REQUEST['content']}</content>".
"</comment>".
"</comments>";
?>
jQuery傳遞參數(shù)是相同的,區(qū)別在于回調函數(shù)對數(shù)據(jù)處理的方式的不同。從PHP代碼中可以看出數(shù)據(jù)是以xml的格式傳入的。
jQuery處理xml就像處理html一樣,可以獲取屬性的值,也可以獲取節(jié)點的值,獲取這些值之后,就可以進行一定的處理,返回到頁面中去。
更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jquery中Ajax用法總結》、《jQuery form操作技巧匯總》、《jQuery常用插件及用法總結》、《jQuery操作json數(shù)據(jù)技巧匯總》、《jQuery擴展技巧總結》、《jQuery拖拽特效與技巧總結》、《jQuery表格(table)操作技巧匯總》、《jQuery常見經(jīng)典特效匯總》、《jQuery動畫與特效用法總結》及《jquery選擇器用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
- jQuery中通過ajax的get()函數(shù)讀取頁面的方法
- jQuery中ajax - get() 方法實例詳解
- Jquery中ajax提交表單幾種方法(get、post兩種方法)
- jQuery使用$.get()方法從服務器文件載入數(shù)據(jù)實例
- jQuery通過控制節(jié)點實現(xiàn)僅在前臺通過get方法完成參數(shù)傳遞
- jQuery中Ajax的get、post等方法詳解
- JQuery.get提交頁面不跳轉的解決方法
- jQuery中get()方法用法實例
- jQuery中ajax的get()方法用法實例
- jQuery中get和post方法傳值測試及注意事項
- jquery中get,post和ajax方法的使用小結
- 快速解決jquery之get緩存問題的最簡單方法介紹
相關文章
jQuery實現(xiàn)倒計時重新發(fā)送短信驗證碼功能示例
這篇文章主要介紹了jQuery實現(xiàn)倒計時重新發(fā)送短信驗證碼功能,結合實例形式分析了基于jQuery的倒計時操作功能實現(xiàn)方法,涉及jQuery表單提交、驗證、正則操作等技巧,需要的朋友可以參考下2017-01-01
使用jQuery模板來展現(xiàn)json數(shù)據(jù)的代碼
通常我們在使用ajax的時候,都避免不了和json這種輕巧的數(shù)據(jù)格式打交道??墒峭謩拥娜ソ馕鰆son,構建HTML,比較麻煩。現(xiàn)在有了這個插件,就能像Extjs那樣使用模板解析json了。2010-10-10
formValidator3.3的ajaxValidator一些異常分析
ajaxvalidator是大家問的最多的問題,修正一個bug(感謝網(wǎng)友“じ龍峸√”),并把大家最關心的問題,再做一次闡述。2011-07-07

