jQuery前臺(tái)數(shù)據(jù)獲取實(shí)現(xiàn)代碼
/// <reference path="jUtil.js" />
$j = jQuery.noConflict();
(function ($j) {
$j.providerData={
defaultSettings: {
select_Span_Items: false,
select_TD_Items: false,
select_LI_Items: false
},
init:function(options){
opts = $j.extend({}, $j.providerData.defaultSettings, options);
singleItemJsonFormat='"{0}":"{1}"';
divItemFormat='{{0}}';
tableItemFormat='{0}';
},
getDataName:function(item){
var dataName = item.attr("dataname");
if(typeof dataName === 'undefined'||dataName==""){
dataName = item.attr("Id");
}
if(typeof dataName === 'undefined'||dataName==""){
dataName=item.text();
}
if(typeof dataName === 'undefined'||dataName==""){
dataName=item[0].innerText;
}
return dataName;
},
getData:function(selector){
//var selector=opts.selector;
var items;
if(typeof selector!=="object"){
items=$j(selector);
}
else{
items=selector;
}
var retJsonValue="";
var subSelector=":input";
if (opts.select_Span_Items) {
subSelector = subSelector + ",span";
}
if (opts.select_LI_Items) {
subSelector = subSelector + ",li";
}
if(opts.select_TD_Items){
subSelector = subSelector + ",td";
}
$j.each(items,function(i,item){
var t=$j(this);
var dataName=$j.providerData.getDataName(t);
//return:'{"dataname":"value"}'
if($j.util.isInput(t)){
var value=t.val();
var itemStr= String.format(singleItemJsonFormat,dataName,value);
retJsonValue=retJsonValue+itemStr+",";
retJsonValue=retJsonValue.trimEnd(",");
}
//return:'{"dataname1":"value1","dataname2":"value2"}'
else if($j.util.isDiv(t)){
var subItems = t.find(subSelector);
var subStr = "";
$j.each(subItems, function (i, subItem) {
//遞歸調(diào)用 處理子元素
var tempSubItemStr = $j.providerData.getData("#"+$j(this).attr("id"));
subStr = subStr + "," + tempSubItemStr;
})
subStr=subStr.trimStart(",");
retJsonValue=String.format(divItemFormat,subStr);
}
//return:'[{"dataname1":"value1","dataname2":"value2"},{"dataname1":"value3","dataname2":"value4"}]'
else if($j.util.isTable(t)){
var trItems=t.find("TR:gt(0)");
//迭代處理Table中的每一行
$j.each(trItems,function(i,trItem){
//處理行,找出行中的要收集數(shù)據(jù)的元素
var subItems =$j(this).find(subSelector);
var subStr = "";
//迭代處理行中所有需要收集數(shù)據(jù)的元素
$j.each(subItems,function(i,subItem){
if($j.util.isInput($j(this))){
var tempSubItemStr = $j.providerData.getData("#"+$j(this).attr("id"));
subStr = subStr + "," + tempSubItemStr;
}
else{
//如果是TD元素,且他里面不包含INPUT元素
if( $j.util.isTD($j(this)) && ($j(this).find(":input").length==0)){
var tempSubItemStr = $j.providerData.getData($j(this));
subStr = subStr + "," + tempSubItemStr;
}
}
});
subStr=subStr.trimStart(",");
retJsonValue= retJsonValue+String.format(divItemFormat,subStr)+",";
});
retJsonValue=retJsonValue.trimEnd(",");
retJsonValue="["+retJsonValue+"]";
}
//return:'{"dataname":"value"}'
else if($j.util.isLi(t)){
var value=t.text();
var itemStr= String.format(singleItemJsonFormat,dataName,value);
retJsonValue=retJsonValue+itemStr+",";
retJsonValue=retJsonValue.trimEnd(",");
}
//return:'{"dataname":"value"}'
else if($j.util.isTD(t)){
var value=t.text();
// var rowIndex = t.parent().prevAll().length;
//獲取當(dāng)前列的列索引
var colIndex = t.prevAll().length;
//dataName由列的的列頭中dataName屬性決定,如果找不到由列頭的內(nèi)容決定
var thItem=t.parent().parent().parent().find("th:eq("+colIndex+")");
if(thItem.length==1){
dataName=$j.providerData.getDataName(thItem);
}
var itemStr= String.format(singleItemJsonFormat,dataName,value);
retJsonValue=retJsonValue+itemStr+",";
retJsonValue=retJsonValue.trimEnd(",");
}
});
return retJsonValue;
}
};
jProviderData=function(options){
$j.providerData.init(options);
return $j.providerData.getData(options.selector);
};
})(jQuery);
String.prototype.trimStart = function(trimStr){
if(!trimStr){return this;}
var temp = this;
while(true){
if(temp.substr(0,trimStr.length)!=trimStr){
break;
}
temp = temp.substr(trimStr.length);
}
return temp;
};
String.prototype.trimEnd = function(trimStr){
if(!trimStr){return this;}
var temp = this;
while(true){
if(temp.substr(temp.length-trimStr.length,trimStr.length)!=trimStr){
break;
}
temp = temp.substr(0,temp.length-trimStr.length);
}
return temp;
};
String.prototype.trim = function(trimStr){
var temp = trimStr;
if(!trimStr){temp=" ";}
return this.trimStart(temp).trimEnd(temp);
};
String.format = function () {
if (arguments.length == 0) {
return null;
}
var str = arguments[0];
for (var i = 1; i < arguments.length; i++) {
var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
str = str.replace(re, arguments[i]);
}
return str;
}
(function ($j) {
$j.util = {
_compareTagName : function (item, tagName) {
return item.attr("tagName").toUpperCase() == tagName.toUpperCase();
},
isTable: function (item) {
return $j.util._compareTagName(item, "TABLE");
},
isDiv: function (item) {
return $j.util._compareTagName(item, "DIV");
},
isInput: function (item) {
return $j.util._compareTagName(item, "INPUT");
},
isSpan: function (item) {
return $j.util._compareTagName(item, "SPAN");
},
isLi: function (item) {
return $j.util._compareTagName(item, "LI");
},
isTD: function (item) {
return $j.util._compareTagName(item, "TD");
}
};
})(jQuery);
相關(guān)文章
jQuery實(shí)現(xiàn)定位滾動(dòng)條位置
基于jQuery實(shí)現(xiàn)滾動(dòng)條滾動(dòng)到子元素位置以此來定位滾動(dòng)條位置,下面是小編給大家?guī)淼膶?shí)現(xiàn)代碼,代碼比較簡單,需要的朋友可以參考下2016-08-08jQuery編輯器KindEditor4.1.4代碼高亮顯示設(shè)置教程
接下來介紹下編輯器KindEditor4.1.4代碼高亮顯示設(shè)置:加載需要的JS和CSS文件/編輯器初始化設(shè)置后,在里面加prettyPrint等等,感興趣的你可以參考下本文2013-03-03jQuery實(shí)現(xiàn)frame之間互通的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)frame之間互通的方法,結(jié)合實(shí)例形式分析了jQuery實(shí)現(xiàn)frame父子框架之間的調(diào)用操作實(shí)現(xiàn)方法,需要的朋友可以參考下2017-06-06jQuery懸停文字提示框插件jquery.tooltipster.js用法示例【附demo源碼下載】
這篇文章主要介紹了jQuery懸停文字提示框插件jquery.tooltipster.js用法,涉及jQuery文字提示框插件的引入與調(diào)用實(shí)現(xiàn)技巧,非常簡單實(shí)用,需要的朋友可以參考下2016-07-07jQuery實(shí)現(xiàn)彈出窗口中切換登錄與注冊(cè)表單
本文給大家推薦的是一款jQuery實(shí)現(xiàn)彈出窗口中切換登錄與注冊(cè)表單的特效。適用瀏覽器:IE8+、FireFox、Chrome、Safari、Opera。十分的方便實(shí)用,有需要的小伙伴可以參考下。2015-06-06jQuery控制文本框只能輸入數(shù)字和字母及使用方法
這篇文章主要介紹了jQuery控制文本框只能輸入數(shù)字和字母及使用方法的相關(guān)資料,非常不錯(cuò)而且實(shí)用性也很高,需要的朋友可以參考下2016-05-05為JQuery EasyUI 表單組件增加焦點(diǎn)切換功能的方法
下面小編就為大家?guī)硪黄獮镴Query EasyUI 表單組件增加焦點(diǎn)切換功能的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04jquery通過a標(biāo)簽刪除table中的一行的代碼
刪除table中的一行的方法有很多,在本文為大家介紹下jquery是如何做到的,下面有個(gè)不錯(cuò)的示例,喜歡的朋友可以參考下2013-12-12通過jquery.cookie.js實(shí)現(xiàn)記住用戶名、密碼登錄功能
這篇文章主要介紹了通過jquery.cookie.js實(shí)現(xiàn)記住用戶名、密碼登錄功能,通過Cookies讓網(wǎng)站服務(wù)器把少量數(shù)據(jù)儲(chǔ)存到客戶端的硬盤或內(nèi)存,從客戶端的硬盤讀取數(shù)據(jù)的一種技術(shù);具體實(shí)現(xiàn)過程大家通過本文一起看看吧2018-06-06