javascript - 循环遍历 html 单元格并在满足条件时执行函数

标签 javascript jquery html

var tableBody = document.getElementById("firstTableBody"),
  secondTable = document.getElementById("secondTable");

function insertRow() {
  var Row = tableBody.insertRow();
  for (var c = 0; c < 3; c += 1) {
    Row.insertCell(c);
  }
  var Fruits = ["Apples", "Oranges", "Strawberries"],
    random_Fruits = Fruits[Math.floor(Math.random() * Fruits.length)];
  Row.cells[0].innerHTML = random_Fruits;
  Row.cells[1].innerHTML = 100;
  var Sellbtn = document.createElement('button');
  Sellbtn.innerHTML = "Sell"
  Sellbtn.onclick = function Sell() {
    if (secondTable.rows.length < 1) {
      var Row = secondTable.insertRow();
      for (var f = 0; f < 2; f += 1) {
        Row.insertCell(f);
      }
      Row.cells[0].innerHTML = this.parentNode.parentNode.cells[0].innerHTML;
      Row.cells[1].innerHTML = this.parentNode.parentNode.cells[1].innerHTML;
    } else {
      for (var i = 0; i < secondTable.rows.length; i += 1) {
        if (secondTable.rows[i].cells[0].innerHTML === this.parentNode.parentNode.cells[0].innerHTML) {
          secondTable.rows[i].cells[1].innerHTML = +this.parentNode.parentNode.cells[1].innerHTML;
        } else {
          var Rowz = secondTable.insertRow();
          for (var k = 0; k < 4; k += 1) {
            Rowz.insertCell(k);
          }

          Rowz.cells[0].innerHTML = this.parentNode.parentNode.cells[0].innerHTML;
          Rowz.cells[1].innerHTML = this.parentNode.parentNode.cells[1].innerHTML;
        }
      }
    }


  }
  Row.cells[2].appendChild(Sellbtn);
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <table border="1">
    <thead>
      <th>Item</th>
      <th>Sold</th>
      <th>
        <button onclick="insertRow()">Insert</button>
      </th>
    </thead>
    <tbody id="firstTableBody">
    </tbody>
  </table>

  <table border="1">
    <thead>
      <th>Item</th>
      <th>Sold</th>
    </thead>
    <tbody id="secondTable">
    </tbody>
  </table>
</body>

我插入了一行随机插入的水果名称和一个动态添加的名为“出售”的按钮。当我单击“销售”时,它应该检查第二个表中是否存在该行的水果名称,如果存在,那么它应该在第二个表中具有相同名称的行中添加销售金额。如果没有,则只需在第二个表中添加一个新行,其中包含名称和销售金额。 jQuery 没问题。

最佳答案

这是一个可能的解决方案,替换您的函数 Sell()

Sellbtn.onclick = function Sell() {

var found = false,
    rows = secondTable.rows,
    numrows = rows.length,
    tofind = this.parentNode.parentNode.cells[0].innerHTML,
    foundin,
    numToAdd = parseInt(this.parentNode.parentNode.cells[1].innerHTML),
    num,
    x;

    for(x=0;x<numrows;x++){

        if(rows[x].cells[0].innerHTML === tofind){

            found = true;

            foundin = x;

        }

    }

    if(found){

        num = parseInt(rows[foundin].cells[1].innerHTML) + numToAdd;

        rows[foundin].cells[1].innerHTML = num;

    }
    else{

        var Row = secondTable.insertRow();
        for (var f = 0; f < 2; f += 1) {
            Row.insertCell(f);
        }
        Row.cells[0].innerHTML = this.parentNode.parentNode.cells[0].innerHTML;
        Row.cells[1].innerHTML = this.parentNode.parentNode.cells[1].innerHTML;

    }



}

关于javascript - 循环遍历 html 单元格并在满足条件时执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28092247/

相关文章:

javascript - 使用 textangular 在内容可编辑的 div 中记录和恢复插入符号的位置

javascript - 为什么使用原型(prototype)而不是方法

javascript - 当用户使用调制解调器时获取用户地理位置

jquery - 先播放 GIF 的一部分,然后无限循环某一部分

javascript - 使用 javascript/jquery 将 html 转换为 svg

javascript - 使用带有 setDragImage 的 Sprite 图像

javascript - 根据屏幕尺寸缩放多个元素

Javascript - 获取文档片段中的元素数量

jquery - 增加 jQuery UI 弹出窗口的大小

javascript - 查询 :not selector doesn't work with style on class