如何根据阈值更改单元格的内容

时间:2019-02-25 03:34:49

标签: javascript html5

因此,对于该应用程序,用户应添加信息,然后该应用程序将计算出他们是人类还是吸血鬼。据我所知,我的逻辑是正确的,但是当我试图根据逻辑替换说吸血鬼的单元格内容时,我什么也没得到。

这是每当用户选择阈值方法时调用的函数,如所附图像所示。但是,当我调用该函数时,没有任何反应。我知道逻辑是正确计算的,因为我做了一些测试,但是在替换包含人类的吸血鬼细胞时遇到了麻烦。

enter image description here

 function Threshold_Process(){
      var totalThreshold = 0;
      var totalOutput;
      var garlic_statement;
      var shadow_statement;
      var pale_statement;
      if(!document.getElementById("galic_checkbox").checked)
      {
        totalThreshold = totalThreshold +3;
      }
      else
      {
        garlic_statement = "The user likes garlic";
      }

      if(!document.getElementById("shadow_checkbox").checked)
      {
        totalThreshold = totalThreshold +4;
      }
      else
      {
        shadow_statement = "The user has a shadow";
      }
      if (document.getElementById("pale_checkbox").checked) 
      {
        totalThreshold = totalThreshold+3;
      }
      if (totalThreshold>6)
      { 
        var i;
        for(i=0; i < id; i++) {
         var change =document.getElementById(id);
         change.insertCell(2).innerHTML= "The Vampire";
         change.deleteCell(3);
        }
      }
      else 
      {
        totalOutput = " The user is not a vampire because:" +shadow_statement + " " + garlic_statement; 
      }
    }

这是我的插入行功能。

<script type="text/javascript">
      var id =1;
    function insert_row(){
      var firstName = document.getElementById('first_name').value;
      var lastName = document.getElementById('last_name').value;

      var table = document.getElementById("saving_table");

      // Create an empty <tr> element and add it to the 1st position of the table:
      var row = table.insertRow(1);

      row.id=id;
      var rowId = row.id;


      // Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
      var cell1 = row.insertCell(0);
      var cell2 = row.insertCell(1);
      var cell3 = row.insertCell(2);

      // Add some text to the new cells:
      cell1.innerHTML = firstName;
      cell2.innerHTML = lastName;
      cell3.innerHTML = "human";
       id++;
    }

以及我的表初始化

 <div class="container">
 <table class="table table-bordered" id="saving_table">
    <caption>Classmates</caption>
    <thead>
      <tr>
        <th>FirstName</th>
        <th>LastName</th>
        <th>Human or Vampire?</th>
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>
  <button class="btn btn-primary" onclick="insert_row()">Submit</button>

0 个答案:

没有答案