无法自动计算Javascript

时间:2016-07-01 02:14:32

标签: javascript jquery html

HTML

<select id="k1" onchange="kes1();" name="kelayakan1" size="1">
  <option value="">-- Pilih Salah Satu --</option>
  <option value="1">Layak</option>
  <option value="0">Tidak Layak</option>
</select>
<br>
<select id="k2" onchange="kes2();" name="kelayakan2" size="1">
  <option value="">-- Pilih Salah Satu --</option>
  <option value="1">Layak</option>
  <option value="0">Tidak Layak</option>
</select>
<br>
<br>
<table style="width:30%;" border="1">
  <thead>
    <tr>
      <th colspan="5">TES</th>
    </tr>
    <tr>
      <th></th>
      <th>A</th>
      <th>B</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>
        <div id="aa1" class="layak" style="text-align:center;"></div>
      </td>
      <td>
        <div id="aa2" class="layak" style="text-align:center;"></div>
      </td>
    </tr>
    <tr>
      <td>2</td>
      <td>
        <div id="bb1" class="layak" style="text-align:center;"></div>
      </td>
      <td>
        <div id="bb2" class="layak" style="text-align:center;"></div>
      </td>
    </tr>
    <tr>
      <td>Result</td>
      <td>
        <div id="hasil1" style="text-align:center;"></div>
      </td>
      <td>
        <div id="hasil2" style="text-align:center;"></div>
      </td>
    </tr>
  </tbody>
</table>

JS

  function kes1() {
    var ap = document.getElementById('k1').value;
    var satu = 1;
    var nol = 0;
    if (ap == "1") {
      document.getElementById('aa1').innerHTML = satu;
      document.getElementById('aa2').innerHTML = nol;
    } else {
      document.getElementById('aa1').innerHTML = nol;
      document.getElementById('aa2').innerHTML = satu;
    }
  }
  function kes2() {
    var bp = document.getElementById('k2').value;
    var satu = 1;
    var nol = 0;

    if (bp == "1") {
      document.getElementById('bb1').innerHTML = satu;
      document.getElementById('bb2').innerHTML = nol;
    } else {
      document.getElementById('bb1').innerHTML = nol;
      document.getElementById('bb2').innerHTML = satu;
    }
  }
  $(document).ready(function() {
    //Iterate through each Textbox and add keyup event handler
    $(".layak").each(function() {
      $(this).innerHTML(function() {
        //Initialize total to 0
        var total = 0;
        $(".layak").each(function() {
          // Sum only if the text entered is number and greater than 0
          if (!isNaN(this.value) && this.value.length != 0) {
            total += parseFloat(this.value);
          }
        });
        //Assign the total to label
        //.toFixed() method will roundoff the final sum to 2 decimal places
        $("#hasil1").val(total);
      });
    });
  });

我想计算A1和A2,B1和B2并出现在结果..

如果1 + 0 = 1,如果1 + 1 = 2,如果0 + 0 = 0

但仍然无法正常运作:(

这个jsfiddle .. https://jsfiddle.net/taraym/57Ltreb0/

你可能知道如何解决它

1 个答案:

答案 0 :(得分:1)

我使用工作版https://jsfiddle.net/57Ltreb0/8/更新了您的问题。

请参阅Is there any function like IsNumeric in javascript to validate numbers以了解我添加的功能isNumeric

让我知道它是否对你有所帮助。