post data from table row like json format

后端 未结 3 826
囚心锁ツ
囚心锁ツ 2021-01-03 15:24

this is related to my last question( NOTE: I already got some good answers there). I\'m doing a program that will filter. I didn\'t include this question because i thought t

3条回答
  •  梦谈多话
    2021-01-03 16:00

    There are two things you need to do here. First get the data into an array of objects, and secondly get the string representation.

    I have not tested this, but it should give you a basic idea of what to do.

    Edit Please take a look at this JS-Fiddle example I've made. http://jsfiddle.net/4Nr9m/52/

    $(document).ready(function() {
        var objects = new Array();
        $('table tr').each(function(key, value) {
            if($(this).find('td:first').not(':empty')) {
            //loop over the cells
            obj = {};
            $(this).find('td').each(function(key, value) {
                    var label = $(this).parents('table').find('th')[key].innerHTML;
                    obj[label] = value.innerHTML;
    
            });
            objects.push(obj);
    
            }
        });
        //get JSON.
        var json = objects.toSource();
        $('#result').append(json);
    });
    

提交回复
热议问题