Javascript 如何从表中获取选定的行值

Javascript 如何从表中获取选定的行值,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,我正在将一个HTML表转换为一个数组,并将其传递给一个控制器以插入多行。我可以创建数组,但问题是它正在创建一个完整的表数组,但我只想获取选定的行td值 //function to convert HTML table to array// var HTMLtbl = { getData: function (table) { var data = []; table.find('tr').not(':first').each(function(rowIn

我正在将一个HTML表转换为一个数组,并将其传递给一个控制器以插入多行。我可以创建数组,但问题是它正在创建一个完整的表数组,但我只想获取选定的行
td

//function to convert HTML table to  array//
var HTMLtbl = {
    getData: function (table) {
        var data = [];
        table.find('tr').not(':first').each(function(rowIndex, r) {
            var cols = [];

            // I believe the main problem is here:
            $(this).find('td').each(function(colIndex, c) {
                cols.push($(this).text().trim());
            });
            data.push(cols);
        });
        return data;
    }
}

$(document).on('click', '#btnsave', function() {
    var data = HTMLtbl.getData($('#tblresult'));
    var parameters = {};
    parameters.array = data;

    var request =  $.ajax({
        async: true,
        cache: false,
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "../Home/Save_SearchCarsDocs",        
        data: JSON.stringify(parameters)
    });
    request.done(function(msg) {
        alert("Row saved " + msg.d);
    });

你可以试试下面

    //function to convert HTML table to  array//
   var excludedTD_Index = [0,5,7]; // define what you want to exclude by index
    var HTMLtbl = {
        getData: function (table) {
            var data = [];
            table.find('tr').not(':first').each(function(rowIndex, r) {
                var cols = [];

                // I believe the main problem is here:
                $(this).find('td').each(function(colIndex, c) {
                    if(excludedTD_Index.indexOf(colIndex) >=0) // exclude TD
                    continue;
                    cols.push($(this).text().trim());
                });
                data.push(cols);
            });
            return data;
        }
    }

    $(document).on('click', '#btnsave', function() {
        var data = HTMLtbl.getData($('#tblresult'));
        var parameters = {};
        parameters.array = data;

        var request =  $.ajax({
            async: true,
            cache: false,
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "../Home/Save_SearchCarsDocs",        
            data: JSON.stringify(parameters)
        });
        request.done(function(msg) {
            alert("Row saved " + msg.d);
        });
或者,如果您可以将以下数据添加到您的TD标签中,则可以使其更易于管理

<td data-exclude="1"> 
...

谢谢Rory McCrossan终于得到了我的答案我在下面添加了我的解决方案

     var CB=1;
var HTMLtbl =
    {
        getData: function (table) {
            var data = [];

            table.find('tr').not(':first').each(function (rowIndex, r) {
                if ($("#chk_" + CB).is(':checked'))
                    {
                    var cols = [];
                    $(this).find('td').each(function (colIndex, c) {                      
                        cols.push($(this).text().trim());     

                    });
                    data.push(cols);
                }
                CB++;
            });
            CB = 1;
            return data;

        }
    }

您如何确切地知道何时选择了一行?看到你的HTML会有很大帮助。表的每一行中是否有复选框或其他元素?您可以使用colIndex属性来标识TD的索引,并可以轻松决定要排除的内容。或者,如果您想要更合适的解决方案,可以向TD添加数据属性以排除该TD。。例如。。它可以用来排除目标TD。。如果您需要此解决方案的代码,请告诉我
     var CB=1;
var HTMLtbl =
    {
        getData: function (table) {
            var data = [];

            table.find('tr').not(':first').each(function (rowIndex, r) {
                if ($("#chk_" + CB).is(':checked'))
                    {
                    var cols = [];
                    $(this).find('td').each(function (colIndex, c) {                      
                        cols.push($(this).text().trim());     

                    });
                    data.push(cols);
                }
                CB++;
            });
            CB = 1;
            return data;

        }
    }