从表格行像JSON格式发布数据 [英] post data from table row like json format

查看:119
本文介绍了从表格行像JSON格式发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我上一次问题有关(
注意:我已经在那里得到了一些很好的答案)。我正在做一个过滤程序。我没有包括这个问题,因为我认为只要我知道如何从行中获取数据,就可以添加文本。但令我沮丧的是,直到现在,我还是无法编写一个好的程序。



我目前使用这个javascript代码(感谢Awea):

  $('#out')。click(function(){
$('table tr')。each(function(){
var td ='';
$(this ).find('option:selected')。each(function(){
td = td +''+ $(this).text();
});
td = td +''+ $(this).find('input').val();
alert(td);
});
})

我的问题是:如何在行数据之前添加文本?例如,这段代码提醒
第一行,如 data1.1 data1.2 data1.3



然后第二行像 data2.1 data2.2 data2.3



我想输出以这样的方式显示

[
{name:data1.1,comparison:data1.2 ,value:data1.3},
{name:data2.1,comparison:data2.2,value:data2.3},
{name:data3.1,比较:data3.2,value:data3.3}
{.....等等...... ...}]



但是在这之前,我想检查一行中的所有FIRST单元是否为空。如果它是空的,跳过那一行,然后进入下一行。



是否有人可以帮助我,请... ...

解决方案

建立在我的回答 传送到您的前面的问题,请参阅 http://jsfiddle.net/evbUa/1/



将数据存入JavaScript对象(在我的示例中为 dataArray )后,您可以自己编写JSON,根据我的示例,但您会发现使用库更容易,例如 JSON-js (另请参见 )。

  //对象来保存您的数据
函数dataRow(value1,value2,value3){
this.name = value1;
this.comparison = value2;
this.value = value3;

$ b $('#out')。click(function(){

//创建数组来保存数据
var dataArray = new Array();

//遍历表的行
for(var i = 1; i< = $(table tr).length; i ++){
$ b $ //检查是否使用第一个字段
if($(table tr:nth-​​child(+ i +)select [class ='field'])。val() .length> 0){

//创建对象并推送到数组
dataArray.push(
new dataRow(
$(table tr:nth- (+ i +)select [class ='field'])。val(),
$(table tr:nth-​​child(+ i +)select [class ='comp' ])。val(),
$(table tr:nth-​​child(+ i +)input).val())
);
}

}

//考虑使用JSON库为你做
(var i = 0; i< dataArray.length; i ++){
var output =;
output = output +'{name:data'+(i + 1)+'。'+ dataArray [i] .name +',';
output = output +'比较:data'+(i + 1)+'。'+ dataArray [i] .comparison +',';
output = output +'value:data'+(i + 1)+'。'+ dataArray [i] .value +'}';
alert(输出);
}
})


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 that it is easier for me to add text as long as i know how to get the data from the row. But to my dismay, I wasn't able to code a good program til now.

Im currently using this javascript code (thanks to Awea):

$('#out').click(function(){    
   $('table tr').each(function(){
      var td = '';
      $(this).find('option:selected').each(function(){
         td = td + ' ' + $(this).text();
      });
      td = td + ' ' + $(this).find('input').val();
      alert(td);
   });
})

my question is: How to add text before the data from the row? like for example, this code alert the first row like data1.1 data1.2 data1.3,

then the second row like data2.1 data2.2 data2.3,

I want my output to be displayed like this

[ {"name":"data1.1","comparison":"data1.2", "value":"data1.3"}, {"name":"data2.1","comparison":"data2.2", "value":"data2.3"}, {"name":"data3.1","comparison":"data3.2", "value":"data3.3"} {.....and so on......}]

but before that happen, i want to check if all the FIRST cell in a row is not empty. if its empty, skip that row then proceed to next row.

is there somebody can help me, please...

解决方案

Building on my answer to your previous question, see http://jsfiddle.net/evbUa/1/

Once you have your data in a javascript object (dataArray in my example), you can write the JSON yourself, per my example, but you will find it much easier to use a library such as JSON-js (see this also).

// object to hold your data
function dataRow(value1,value2,value3) {
    this.name = value1;
    this.comparison = value2;
    this.value = value3;
}

$('#out').click(function(){   

    // create array to hold your data
    var dataArray = new Array();

    // iterate through rows of table
    for(var i = 1; i <= $("table tr").length; i++){

        // check if first field is used
        if($("table tr:nth-child(" + i + ") select[class='field']").val().length > 0) {

            // create object and push to array
            dataArray.push(    
                new dataRow(
                    $("table tr:nth-child(" + i + ") select[class='field']").val(),
                    $("table tr:nth-child(" + i + ") select[class='comp']").val(),
                    $("table tr:nth-child(" + i + ") input").val())
            );
        }

    }

    // consider using a JSON library to do this for you
    for(var i = 0; i < dataArray.length; i++){
        var output = "";
        output = output + '{"name":"data' + (i + 1) + '.' + dataArray[i].name + '",';
        output = output + '"comparison":"data' + (i + 1) + '.' + dataArray[i].comparison + '",';
        output = output + '"value":"data' + (i + 1) + '.' + dataArray[i].value + '"}';
        alert(output);
    }
})

这篇关于从表格行像JSON格式发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆