jquery将表格数据封装到数组

公用方法:

var json = new Array();

第一种是多表头:


function tabToJSONForJquery(id) { //id为表格的id
    json.splice(0);

    //我这前三行是表头所以用eq()方法去获取行号
    json = getJsonInfo(id,'eq(0)','th',json);
    json = getJsonInfo(id,'eq(1)','th',json);
    json = getJsonInfo(id,'eq(2)','th',json);

    //以下是获取表格数据行,(.not-box)是某行引用了这个类样式,只要是引用了这个类的就不获取它的数据
    json = getJsonInfo(id,'not(.not-box)','td',json);
}
function getJsonInfo(id,v1,v2,json){
    var tt = $("#" + id).find("tr:"+v1).map(function(i, e) {
        return json.push($(e).children(v2).map(function(j, el) {
          return $(el).text();
        }).get());
    });
    return json;
}

第二种是单表头(第一行是表头):

function tabToJSONForJquery(id) {                                      //id为表格的id
    json.splice(0);

    $("#" + id).find("tr:first th").map(function(i, e) {  //获取表头数据
          return json.push($(e).children(th).map(function(j, el) {
              return $(el).text();
        }).get());
    });

    $("#" + id).find("tr:not(:first)).map(function(i, e) {
          return json.push($(e).children(td).map(function(j, el) {
              return $(el).text();
        }).get());
    });
}

猜你喜欢

转载自blog.csdn.net/z19799100/article/details/105854669
今日推荐