0

I'm trying to build and array with the values of the table row that the user clicks.

so right now I have all of this inside a

$('#myDiv').find('tr').click(function(){

and assign variables to columns in the table on the row the user clicks

var firstName = $(this).find('td:nth-child(1)').text();     
var middleName = $(this).find('td:nth-child(2)').text();        
var lastName = $(this).find('td:nth-child(3)').text();

so now I'm trying to put those in an array

var userRow = [];
userRow['firstName'] = firstName;
userRow['middleName'] = middleName;
userRow['lastName'] = lastName;

so the thing is, the user is able to click on multiple table rows. When the click a row it toggles a class 'highlight' in the row (which changes the background color of the row)

I'm stuck on creating a loop so that all of the rows they click get added into another array. Kind of like this-

Array1 {
      userRow {
            'firstName' = ...;
             ect...
      }
}

so when I reference Array1 it'll show me all of the information that I've grabbed from all of the rows with the 'highlight' class, which I've put in the array 'userRow'

I've read on javascript arrays, but like I said I'm stuck and could use some help. It is much appreciated, thank you.

EDIT: I'm leaving right now, I will be back on tomorrow to choose an answer. thanks all of you for helping me understand this

4

4 回答 4

1

Javascript 没有关联数组。看起来像关联数组的语法实际上是对象的括号表示法。所以myObj.prop1myObj['prop1']完全一样。

我想你想要这样的东西:

var selectedRows = $.map($('#myDiv tr.highlight'), function(row, idx) {     
    $row = $(row);
    return {
        firstName : $row.find('td:nth-child(1)').text(),   
        middleName : $row.find('td:nth-child(2)').text(),   
        lastName : $row.find('td:nth-child(3)').text()
    };
});

selectedRows将是一个数组,其中包含许多对象,其键firstName为 、middleNamelastName.

于 2013-08-29T18:11:34.153 回答
1

JS-array 不支持字符串键。改为使用对象:

var userRow = {};
userRow.firstName = firstName;
userRow.middleName = middleName;
userRow.lastName = lastName;

之后,您可以推userRow送到Array1

Array1 = [];
Array1.push(userRow);

或者再次使用对象:

WrapObj = {};   // like Array1 but object
WrapObj.someId = userRow;
于 2013-08-29T18:13:11.643 回答
1

javascript 中的数组是索引的,而不是关联的。然而,javascript 对象的行为很像关联数组。因此,您可以这样做:

var userRow = {}; // curly brackets!
userRow['firstName'] = firstName;
userRow['middleName'] = middleName;
userRow['lastName'] = lastName;

然后,您通常可以将此 userRow 对象添加到另一个数组中:

var Array1 = [];
Array1.push(userRow);
于 2013-08-29T18:11:01.013 回答
1

我认为你可以使用类似的东西。

我将按照您编写的方式编写一些简单的代码,以便您轻松理解它。

var arr;

$('#myDiv').find('tr').click(function(){

   // initialize array
   if (typeof(arr) == 'undefined')) {
       arr = new Array();
   }); 

   // read values
   var firstName = $(this).find('td:nth-child(1)').text();     
   var middleName = $(this).find('td:nth-child(2)').text();        
   var lastName = $(this).find('td:nth-child(3)').text();

   // create your object
   var yourObject = {
       'firstName' : firstName,
       'middleName' : middleName,
       'lastName' : lastName
   };

   // add your object to the array
   arr.push(yourObject); 
});
于 2013-08-29T18:17:04.410 回答