解析Jquery Json Php代碼相關(guān)使用問題
下面文章總結(jié)一下Jquery Json Php代碼非常好用,只要返回json數(shù)據(jù)就可以了,如果對(duì)json數(shù)據(jù)不太了解請(qǐng)先百度或者google一下 。下面是一個(gè)我的工作實(shí)例;希望大家能夠?qū)W習(xí)參考。
jQuery擴(kuò)展ajaxName,用于ajax提交方法的頁(yè)面數(shù)據(jù)獲取,根據(jù)頁(yè)面元素的name生成相應(yīng)的js對(duì)象,然后可以把這個(gè)對(duì)象傳給Jquery Json Php代碼庫(kù)進(jìn)行編碼向后臺(tái)提交,支持?jǐn)?shù)組,支持.表達(dá)式,#t#
下面就對(duì)Jquery Json Php代碼介紹:
- /*
- * jQuery Ajax By Name Plugin
- *
- * licensed under the MIT licenses:
- * http://www.opensource.org/licenses/mit-license.php
- *
- * Author achun (achun.shx at gmail.com)
- * Create Date: 2008-6-21
- * Last Date: 2008-6-21
- * Revision:2.8.6.21
- */
- (function($) {
- /**
- * 獲取鼠標(biāo)點(diǎn)擊的元素坐標(biāo)
- */
- $.fn.clickPos = function(e){
- var pos ={x:0,y:0};
- if (e.offsetX != undefined) {
- pos.x = e.offsetX;
- pos.y = e.offsetY;
- } else if (typeof $.fn.offset == 'function') {
- var offset = this.offset();
- pos.x = e.pageX - offset.left;
- pos.y = e.pageY - offset.top;
- } else {
- pos.x = e.pageX - this[0].offsetLeft;
- pos.y = e.pageY - this[0].offsetTop;
- }
- return pos;
- }
- /**
- * ajaxName() 根據(jù)標(biāo)簽name屬性來提交數(shù)據(jù),替代form提交方式
- * options:{//與ajax的options是一樣的結(jié)構(gòu)
- * data:{}//要附加提交的數(shù)據(jù)
- * }
- * successful:如果元素有有效值,defualt:true
- */
- $.fn.ajaxName = function(options,successful) {
- if(options==undefined) options={};
- optionsoptions.data = options.data || {};
- successfulsuccessful = successful == undefined ? true:successful;
- var data = {};
- var name= options.name || '';
- this.each(function() {
- var el=this,o=$(this),n = o.attr('name'),hasval = o.attr('value')!=undefined;
- if (!n) return;
- var t = el.type, tag = el.tagName.toLowerCase();
- if (n=='/'){
- if (name=='') name = hasval?o.attr('value'):o.text();
- return;
- }
- if (successful && (el.disabled || t == 'reset' || t == 'button' ||
- (t == 'checkbox' || t == 'radio') && !el.checked ||
- (t == 'submit' || t == 'image') ||
- tag == 'select' && el.selectedIndex == -1))
- return;
- var v= hasval? o.attr('value') :o.hasClass('ValueByText')?o.text():o.html();
- if (v == undefined) return;
- if (n.indexOf('.')>0){
- nn=n.split('.');
- var len=n.length - 1;
- var b=data;
- $.each(n,function(i){
- if (i!=len){
- if(b[this] == undefined) b[this]={};
- bb=b[this];
- }else
- b[this]=v;
- });
- }else if (n.indexOf('[]')>0){
- nn=n.slice(0,n.length-2);
- if(data[n] == undefined) data[n]=[];
- data[n].push(v);
- }else{
- data[n]=v;
- }
- });
- var dat={};
- if (name){
- dat[name]=data;
- datdata=dat[name];
- }else
- dat=data;
- for (var o in options.data){
- data[o]=options.data[o];
- };
- options.data=dat;
- return options.data;
- };
- })(jQuery);
- /*
- * jQuery Ajax By Name Plugin
- *
- * licensed under the MIT licenses:
- * http://www.opensource.org/licenses/mit-license.php
- *
- * Author achun (achun.shx at gmail.com)
- * Create Date: 2008-6-21
- * Last Date: 2008-6-21
- * Revision:2.8.6.21
- */
- (function($) {
- /**
- * 獲取鼠標(biāo)點(diǎn)擊的元素坐標(biāo)
- */
- $.fn.clickPos = function(e){
- var pos ={x:0,y:0};
- if (e.offsetX != undefined) {
- pos.x = e.offsetX;
- pos.y = e.offsetY;
- } else if (typeof $.fn.offset == 'function') {
- var offset = this.offset();
- pos.x = e.pageX - offset.left;
- pos.y = e.pageY - offset.top;
- } else {
- pos.x = e.pageX - this[0].offsetLeft;
- pos.y = e.pageY - this[0].offsetTop;
- }
- return pos;
- }
- /**
- * ajaxName() 根據(jù)標(biāo)簽name屬性來提交數(shù)據(jù),替代form提交方式
- * options:{//與ajax的options是一樣的結(jié)構(gòu)
- * data:{}//要附加提交的數(shù)據(jù)
- * }
- * successful:如果元素有有效值,defualt:true
- */
- $.fn.ajaxName = function(options,successful) {
- if(options==undefined) options={};
- optionsoptions.data = options.data || {};
- successfulsuccessful = successful == undefined ? true:successful;
- var data = {};
- var name= options.name || '';
- this.each(function() {
- var el=this,o=$(this),n = o.attr('name'),hasval = o.attr('value')!=undefined;
- if (!n) return;
- var t = el.type, tag = el.tagName.toLowerCase();
- if (n=='/'){
- if (name=='') name = hasval?o.attr('value'):o.text();
- return;
- }
- if (successful && (el.disabled || t == 'reset' || t == 'button' ||
- (t == 'checkbox' || t == 'radio') && !el.checked ||
- (t == 'submit' || t == 'image') ||
- tag == 'select' && el.selectedIndex == -1))
- return;
- var v= hasval? o.attr('value') :o.hasClass('ValueByText')?o.text():o.html();
- if (v == undefined) return;
- if (n.indexOf('.')>0){
- nn=n.split('.');
- var len=n.length - 1;
- var b=data;
- $.each(n,function(i){
- if (i!=len){
- if(b[this] == undefined) b[this]={};
- bb=b[this];
- }else
- b[this]=v;
- });
- }else if (n.indexOf('[]')>0){
- nn=n.slice(0,n.length-2);
- if(data[n] == undefined) data[n]=[];
- data[n].push(v);
- }else{
- data[n]=v;
- }
- });
- var dat={};
- if (name){
- dat[name]=data;
- datdata=dat[name];
- }else
- dat=data;
- for (var o in options.data){
- data[o]=options.data[o];
- };
- options.data=dat;
- return options.data;
- };
- })(jQuery);