在所有行中乘以Cell [2]单元格[3]。细胞[2] *细胞[3]。两个单元格都是Dropbox

时间:2017-03-21 10:26:20

标签: javascript html css calculator

GPAcalculator。所以我需要将单元格[2]和单元格[3]相乘,然后添加结果。但这两个细胞都是滴水盒。那么如何在同一行的单元格[2]和单元格[3]中获取dropbox的id。注意:允许动态添加行。



replicas: 2

function multiplyadd() {
  var tbody = document.getElementById("gpatable");
  var answer = 0,
    answer1 = 0;
  for (var i = 0; i < tbody.rows.length; i++) {
    var row = tbody.rows[i + 1];
    var idd = document.getElementById("gpatable").rows[i + 1].cells[2].id;
    var strUser = idd.options[idd.selectedIndex].value;
    // var credits = row.cells[2].childNodes[0].options[selectedIndex].value;
    var credits = parseInt(strUser);
    var id1 = document.getElementById(row.cells[3].childNodes[0]);
    var grades = parseInt(id1);
    // var grades = row.cells[3].childNodes[0].options[selectedIndex].value;
    var answer1 = (Number(credits) * Number(grades)).toFixed(3);
    answer = answer + answer1;
  }
  alert(answer);
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您需要获取类名为custom-select的所有元素。 getElementsByClassName将返回一个包含2个元素的数组。现在,您可以按索引访问特定值。

var dd = document.getElementsByClassName('custom-select'); //this will get elements with class custom-select
var idxc = dd[0].options[dd[0].selectedIndex].value; // this will get Credits element value
var idxg = dd[1].options[dd[1].selectedIndex].value; // this will get Grade element value

idxc将具有Credits值,而idxg将具有Grade值。

编辑:
总结你需要的所有东西:

document.getElementById("calc").addEventListener("click", function() {
  var dd = document.getElementsByClassName('custom-select');
  var sum = 0;
  if (dd) { // if custom-select element found
    for (var i = 0; i < dd.length - 1; i = i + 2) {
      if (!isNaN(dd[i].options[dd[i].selectedIndex].value) && !isNaN(dd[i + 1].options[dd[i + 1].selectedIndex].value)) {
        sum += dd[i].options[dd[i].selectedIndex].value * dd[i + 1].options[dd[i + 1].selectedIndex].value;
      } else {
        console.log("ERR: Please select all values");
        i = dd.length; // break loop
        sum = 0; // in case of error
      }
    }
  }
  console.log(sum);
}, false);

fiddle DEMO