如何将分割数组函数

时间:2015-11-17 02:00:07

标签: javascript html arrays split

我想将数据添加到表中,将文本拆分为数组。当我点击添加按钮时,数据显示在表格中,并自动将价格和金额乘以coloumn总价。有人可以帮帮我吗?

enter image description here

这是html

    <div class="container">

        <div class="form" style="margin-top: 50px;">

            <div class="form">
                <div class="form-group">
                    <label for="inputEmail3">Input</label>
                    <div class="">
                        <input type="text" class="form-control" id="transaction" placeholder="Input Transaction">
                    </div>
                </div>
                <div class="form-group">
                    <div>
                        <button type="submit" class="btn btn-default" onclick="addTransaction()">Add</button>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <table id="table_trans" class="table table-bordered">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Price</th>
                        <th>Amount</th>
                        <th>Total Price</th>
                    </tr>
                </thead>
                <tbody>

                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="3">
                            <input type="button" value="Total Price" class="btn btn-success" id="sumTransaction()"/>
                        </td>
                        <td id="area_total"></td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
    <!-- /.container -->

这是拆分功能

 function addRow(tags){
 var theTable = document.getElementById('table_trans').getElementsByTagName('tbody')[0];
 var newRow = theTable.insertRow(-1);
 var newCell, theText, i;
 for(i=0;i<tags.length;i++){
 newCell = newRow.insertCell(i);

 theText = document.createTextNode(tags[i]);
 newCell.appendChild(theText);
 }
 }

function addTransaction(){
var inputTags = document.getElementById('transaction').value;
addRow(inputTags.split(','));
}

1 个答案:

答案 0 :(得分:1)

在此codepen中,我将字符串传递给addRowsplit(),然后将PriceAmount相乘获取Total,然后将Total的值附加到tags数组:

function addRow(tags) {
  tags = tags.split(',');
  var total = tags[1] * tags[2];
  tags.push(total);
  var theTable = document.getElementById('table_trans').getElementsByTagName('tbody')[0];
  var newRow = theTable.insertRow(-1);
  var newCell, theText, i;
  for (i = 0; i < tags.length; i++) {
    newCell = newRow.insertCell(i);

    theText = document.createTextNode(tags[i]);
    newCell.appendChild(theText);
  }
}

function addTransaction() {
  var inputTags = document.getElementById('transaction').value;
  addRow(inputTags);
}
<div class="container">

  <div class="form" style="margin-top: 50px;">

    <div class="form">
      <div class="form-group">
        <label for="inputEmail3">Input</label>
        <div class="">
          <input type="text" class="form-control" id="transaction" placeholder="Input Transaction">
        </div>
      </div>
      <div class="form-group">
        <div>
          <button type="submit" class="btn btn-default" onclick="addTransaction()">Add</button>
        </div>
      </div>
    </div>
  </div>

  <div class="row">
    <table id="table_trans" class="table table-bordered">
      <thead>
        <tr>
          <th>Name</th>
          <th>Price</th>
          <th>Amount</th>
          <th>Total Price</th>
        </tr>
      </thead>
      <tbody>

      </tbody>
      <tfoot>
        <tr>
          <td colspan="3">
            <input type="button" value="Total Price" class="btn btn-success" id="sumTransaction()" />
          </td>
          <td id="area_total"></td>
        </tr>
      </tfoot>
    </table>
  </div>
</div>
<!-- /.container -->