开发者

compute data from an array

I have this (simple) question regarding an array. I am trying to get the total of all the board-feet that was entered in the html. Here's a running 开发者_StackOverflow中文版sample code: click here

(Thanks to AsKaiser for that code).

Now, what I want to do is that everytime I click the go button, it will automatically compute that total board-feet that was being entered in that array. So, everytime I input something in that form and clicked go, the data will go to that array and the total of all board-feet will be displayed in the input box below. It will constantly change as I add data to my array.

I can't get it to work and always have a NaN value for my total.


Your code is very confusing. What is the point of plank_data?

Why don't you do this instead, it makes more sense.

function Plank()
{
  var self = this;
  self.Number = 0;
  self.Thickness = 0;
  self.Width = 0;
  self.Length = 0;
  self.Quantity = 0;

  self.GetBoardFoot = function()
  {
    return (((parseFloat(self.Thickness) * parseFloat(self.Width) * parseFloat(self.Length) * parseFloat(self.Quantity)) / 12);
  }
}

var planks = [];
function AddPlank()
{
  var myPlank = new Plank();
  myPlank.Number = $('#plank_number').val();
  myPlank.Thickness = $('#thickness').val();
  myPlank.Width = $('#width').val();
  myPlank.Length = $('#length_t').val();
  myPlank.Quantity = $('#quantity').val();
  planks.push(myPlank);
}

function GetTotalBoardPlank()
{
  var total = 0;
  for (var i = 0; i < planks.length; i++)
    total += planks[i].GetBoardFoot();
  return total;
}

function Go()
{
  AddPlank();
  $('#board_foot').val(parseFloat(GetTotalBoardPlank()));
}


Is this what you are looking for?

plank_data = [];
$('#go').click(function() {

    var plankNum = parseFloat($('#plank_number').val() || 0.00),
        thick = parseFloat($('#thickness').val() || 0.00),
        width = parseFloat($('#width').val() || 0.00),
        length_t = parseFloat($('#length_t').val() || 0.00),
        quantity = parseFloat($('#quantity').val() || 0.00);

    //compute board foot
    board_foot = parseFloat((thick * width * length_t * quantity) / 12);

    // show board foot
    $('#board_foot').val(board_foot);

    var current_data = {};

    $('#plank_form input:text:not("#board_foot")').each(function() {
        var $this = $(this);
        current_data[$this.attr('id')] = parseFloat($this.val() || 0.00);
        $this.val('');
    });

    //this will push data/add data to array
    plank_data.push(current_data);

    var total = parseFloat(plankNum + thick + width + length_t + quantity);
    $('#total').val(total);
});


See this: http://jsfiddle.net/ghostoy/8xFFH/195/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜