jQuery擴(kuò)展方法實(shí)現(xiàn)Form表單與Json互相轉(zhuǎn)換的實(shí)例代碼
JQuery筆記
記兩段代碼,使用JQuery實(shí)現(xiàn)從表單獲取json與后端交互,以及把后端返回的json映射到表單相應(yīng)的字段上。
把表單轉(zhuǎn)換出json對(duì)象
//把表單轉(zhuǎn)換出json對(duì)象 $.fn.toJson = function () { var self = this, json = {}, push_counters = {}, patterns = { "validate": /^[a-zA-Z][a-zA-Z0-9_]*(?:\[(?:\d*|[a-zA-Z0-9_]+)\])*$/, "key": /[a-zA-Z0-9_]+|(?=\[\])/g, "push": /^$/, "fixed": /^\d+$/, "named": /^[a-zA-Z0-9_]+$/ }; this.build = function (base, key, value) { base[key] = value; return base; }; this.push_counter = function (key) { if (push_counters[key] === undefined) { push_counters[key] = 0; } return push_counters[key]++; }; $.each($(this).serializeArray(), function () { // skip invalid keys if (!patterns.validate.test(this.name)) { return; } var k, keys = this.name.match(patterns.key), merge = this.value, reverse_key = this.name; while ((k = keys.pop()) !== undefined) { // adjust reverse_key reverse_key = reverse_key.replace(new RegExp("\\[" + k + "\\]$"), ''); // push if (k.match(patterns.push)) { merge = self.build([], self.push_counter(reverse_key), merge); } // fixed else if (k.match(patterns.fixed)) { merge = self.build([], k, merge); } // named else if (k.match(patterns.named)) { merge = self.build({}, k, merge); } } json = $.extend(true, json, merge); }); return json; };
將josn對(duì)象賦值給form,使表單控件也顯示相應(yīng)的狀態(tài)
//將josn對(duì)象賦值給form $.fn.loadData = function (obj) { var key, value, tagName, type, arr; this.reset(); for (var x in obj) { if (obj.hasOwnProperty(x)) { key = x; value = obj[x]; this.find("[name='" + key + "'],[name='" + key + "[]']").each(function () { tagName = $(this)[0].tagName.toUpperCase(); type = $(this).attr('type'); if (tagName == 'INPUT') { if (type == 'radio') { if ($(this).val() == value) { $(this).attr('checked', true); } } else if (type == 'checkbox') { arr = value.split(','); for (var i = 0; i < arr.length; i++) { if ($(this).val() == arr[i]) { $(this).attr('checked', true); break; } } } else { $(this).val(value); } } else if (tagName == 'SELECT' || tagName == 'TEXTAREA') { $(this).val(value); } }); } } }
補(bǔ)充:下面看下jQuery兩種擴(kuò)展方法
在jQuery中,有兩種擴(kuò)展方法
1.類方法($.extend())
<script> $.extend({ print1:function(name){ //print1是自己定義的函數(shù)名字,括號(hào)中的name是參數(shù) console.log(name) } }); $.print1("坤") ; //調(diào)用時(shí)直接$.函數(shù)名(參數(shù)); </script>
2.對(duì)象方法($.fn.extend())
<body> <input type="text"> <script> $.fn.extend({ getMax:function(a,b){ var result=a>b?a:b; console.log(result); } }); $("input").getMax(1,2); //調(diào)用時(shí)要$(標(biāo)簽名).函數(shù)名(); </script> </body>
3.一般情況下,jQuery的擴(kuò)展方法寫在自執(zhí)行匿名函數(shù)中(原因:在js中是以函數(shù)為作用域的,在函數(shù)中寫可以避免自己定義的函數(shù)或者變量與外部沖突)
<script> (function(){ $.extent({ print1:function(){ console.log(123); } }) })(); </script>
總結(jié)
以上所述是小編給大家介紹的jQuery擴(kuò)展方法實(shí)現(xiàn)Form表單與Json互相轉(zhuǎn)換的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
jquery 新手學(xué)習(xí)常見問(wèn)題解決方法
jquery 新手入門參考教程。2010-04-04jquery select 設(shè)置默認(rèn)選中的示例代碼
本篇文章主要是對(duì)jquery select 設(shè)置默認(rèn)選中的示例代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02用jquery的方法制作一個(gè)簡(jiǎn)單的導(dǎo)航欄
用jquery制作一個(gè)簡(jiǎn)單的導(dǎo)航欄,使用到了addClass及removeClass等方法,需要的朋友可以參考下2014-06-06jQuery動(dòng)態(tài)設(shè)置form表單的enctype值(實(shí)現(xiàn)代碼)
本篇文章是對(duì)在jQuery中動(dòng)態(tài)設(shè)置form表單的enctype值的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07jQuery實(shí)現(xiàn)炫麗的3d旋轉(zhuǎn)星空效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)炫麗的3d旋轉(zhuǎn)星空效果,涉及jQuery數(shù)值運(yùn)算與頁(yè)面元素屬性動(dòng)態(tài)變換相關(guān)操作技巧,需要的朋友可以參考下2018-07-07jQuery簡(jiǎn)單實(shí)現(xiàn)iframe的高度根據(jù)頁(yè)面內(nèi)容自適應(yīng)的方法
這篇文章主要介紹了jQuery簡(jiǎn)單實(shí)現(xiàn)iframe的高度根據(jù)頁(yè)面內(nèi)容自適應(yīng)的方法,給出了2種簡(jiǎn)單實(shí)現(xiàn)方法,涉及jQuery針對(duì)頁(yè)面高度的動(dòng)態(tài)獲取與設(shè)置相關(guān)技巧,需要的朋友可以參考下2016-08-08jquery實(shí)現(xiàn)彈窗功能(窗口居中顯示)
本文主要介紹了jquery實(shí)現(xiàn)彈窗功能且彈出確認(rèn)框始終位于窗口中間位置的實(shí)例,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02jQuery中:first-child選擇器用法實(shí)例
這篇文章主要介紹了jQuery中:first-child選擇器用法,實(shí)例分析了:first-child選擇器的功能、定義及匹配父元素的第一個(gè)子元素的用法技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12