javascript - 如何从拆分数组函数中乘以数据

标签 javascript html arrays split

我想将数据添加到表中,将文本拆分为数组。当我点击添加按钮时,数据出现在表格中,并自动将价格和金额乘以总价。谁能帮帮我?

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(','));
}

最佳答案

如本 codepen ,我将字符串传递给 addRowsplit() 那里,取 Price 并将其乘以 Amount 获取 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 -->

关于javascript - 如何从拆分数组函数中乘以数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33747915/

相关文章:

javascript - 将图像从 XML 显示为 HTML

c - 使用指针对 C 中的结构数组进行排序

javascript - 为什么 Array.prototype.join() 会展平任意深度的数组?

arrays - 在swift数组中查找最大值的索引

javascript - Firebase:如何从服务器上的用户名和密码生成访问 token ?

javascript - 在DOM中,ID不能在 "for"循环中使用,因为ID在一个页面中只能使用一次,请问如何解决?

html - 使用 "background-position"但我的背景图像正在消失

javascript - HTML/CSS : How to make multiple 'versions' of an 'article' for different types of articles

javascript - 防止输入开头出现空格

javascript - RegExp 忽略 <code> 和 <pre> 标签之间的所有内容