Javascript,计算后出现结论

时间:2016-08-24 06:31:10

标签: javascript jquery

我有代码..

计算变量,但经过计算我想加上结论..

e.g。 if Hasil: Layak = 4 or 3,结论是"Layak"

e.g。 if Hasil: Layak = 1 or 2,结论是"Tidak Layak"

结论添加最后一行

https://jsfiddle.net/taraym/13nz8cys/2/

我可以知道怎么做吗?

HTML文件



       $(document).ready(function() {
         //Iterate through each Textbox and add keyup event handler
         $(".tes1").each(function() {
           $(this).change(function() {
             //Initialize total to 0
             var satu = 1;
             var nol = 0;
             var ap = document.getElementById('k1').value;

             var aa1 = document.getElementById('aa1');
             var aa2 = document.getElementById('aa2');
             if (ap == "1") {
               aa1.innerHTML = satu;
               aa2.innerHTML = nol;
             } else {
               aa1.innerHTML = nol;
               aa2.innerHTML = satu;
             }
             calculateSum();
           });
         });

         $(".tes2").each(function() {
           $(this).change(function() {
             //Initialize total to 0
             var satu = 1;
             var nol = 0;
             var bp = document.getElementById('k2').value;

             var bb1 = document.getElementById('bb1');
             var bb2 = document.getElementById('bb2');
             if (bp == "1") {
               bb1.innerHTML = satu;
               bb2.innerHTML = nol;
             } else {
               bb1.innerHTML = nol;
               bb2.innerHTML = satu;
             }
             calculateSum();
           });
         });

         $(".tes3").each(function() {
           $(this).change(function() {
             //Initialize total to 0        
             var satu = 1;
             var nol = 0;
             var cp = document.getElementById('k3').value;

             var cc1 = document.getElementById('cc1');
             var cc2 = document.getElementById('cc2');
             if (cp == "1") {
               cc1.innerHTML = satu;
               cc2.innerHTML = nol;
             } else {
               cc1.innerHTML = nol;
               cc2.innerHTML = satu;
             }
             calculateSum();
           });
         });

         $(".tes4").each(function() {
           $(this).change(function() {
             //Initialize total to 0
             var satu = 1;
             var nol = 0;
             var dp = document.getElementById('k4').value;

             var dd1 = document.getElementById('dd1');
             var dd2 = document.getElementById('dd2');
             if (dp == "1") {
               dd1.innerHTML = satu;
               dd2.innerHTML = nol;
             } else {
               dd1.innerHTML = nol;
               dd2.innerHTML = satu;
             }
             calculateSum();
           });
         });

       });

       function calculateSum() {
         var A = [];
         var sumOfA = 0;
         $('#tabel_data tr:not(:has(th))').not(':last').each(function() {
           A.push($(this).find("td:eq(1)").text().trim());
         })

         for (i = 0; i < A.length; ++i) {
           if (A[i] != '') {
             sumOfA += parseInt(A[i]);
           }
         }
         $('#tabel_data tr:last').find('td:eq(1)').text(sumOfA);


         var B = [];
         var sumOfB = 0;
         $('#tabel_data tr:not(:has(th))').not(':last').each(function() {
           B.push($(this).find("td:eq(2)").text().trim());
         })

         for (i = 0; i < B.length; ++i) {
           if (B[i] != '') {
             sumOfB += parseInt(B[i]);
           }
         }
         $('#tabel_data tr:last').find('td:eq(2)').text(sumOfB);
       }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<table border="1">
  <tbody>
    <tr>
      <td>Kesimpulan I<span class="red">*</span></td>
      <td>
        <select id="k1" class="tes1" name="kelayakan1" size="1">
          <option value="">-- Pilih Salah Satu --</option>
          <option value="1">Layak</option>
          <option value="0">Tidak Layak</option>
        </select>
      </td>
    </tr>

    <tr>
      <td>Kesimpulan II<span class="red">*</span></td>
      <td>
        <select id="k2" class="tes2" name="kelayakan2" size="1">
          <option value="">-- Pilih Salah Satu --</option>
          <option value="1">Layak</option>
          <option value="0">Tidak Layak</option>
        </select>
      </td>
    </tr>

    <tr>
      <td>Kesimpulan III<span class="red">*</span></td>
      <td>
        <select id="k3" class="tes3" name="kelayakan3" size="1">
          <option value="">-- Pilih Salah Satu --</option>
          <option value="1">Layak</option>
          <option value="0">Tidak Layak</option>
        </select>
      </td>
    </tr>



    <tr>
      <td>Kesimpulan IV<span class="red">*</span></td>
      <td>
        <select id="k4" class="tes4" name="kelayakan4" size="1">
          <option value="">-- Pilih Salah Satu --</option>
          <option value="1">Layak</option>
          <option value="0">Tidak Layak</option>
        </select>
      </td>
    </tr>




  </tbody>
</table>




<table style="width:100%;" id="tabel_data" border="1">
  <thead>
    <tr>
      <th colspan="5">Rekapitulasi Kelayakan</th>
    </tr>
    <tr>
      <th>Parameter</th>
      <th>Layak</th>
      <th>Tidak Layak</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="text-align:left;">Indeks Rumah</td>
      <td>
        <div id="aa1" style="text-align:center;"></div>
      </td>
      <td>
        <div id="aa2" style="text-align:center;"></div>
      </td>
    </tr>
    <tr>
      <td style="text-align:left;">Indeks Kepemilikan Harta</td>
      <td>
        <div id="bb1" style="text-align:center;"></div>
      </td>
      <td>
        <div id="bb2" style="text-align:center;"></div>
      </td>
    </tr>
    <tr>
      <td style="text-align:left;">Indeks Pendapatan Keluarga</td>
      <td>
        <div id="cc1" style="text-align:center;"></div>
      </td>
      <td>
        <div id="cc2" style="text-align:center;"></div>
      </td>
    </tr>
    <tr>
      <td style="text-align:left;">Indeks Pengeluaran Harta</td>
      <td>
        <div id="dd1" style="text-align:center;"></div>
      </td>
      <td>
        <div id="dd2" style="text-align:center;"></div>
      </td>
    </tr>
    <tr>
      <td align="center">Hasil</td>
      <td>
        <div id="hasil1" style="text-align:center;"></div>
      </td>
      <td>
        <div id="hasil2" style="text-align:center;"></div>
      </td>
    </tr>

  </tbody>
</table>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

你的问题很多错字。甚至你的代码都是多余的:

试试我的代码:

$(document).ready(function() {
    function testKelayakan(a, b, c) {
        var d = $(a).val();
        if (d) {
            $(b).text(d == 1 ? 1 : 0);
            $(c).text(d == 0 ? 1 : 0)
        }
        evaluasiHasilKelayakan()
    }

    function evaluasiHasilKelayakan() {
        var a = $('#tabel_data tbody');
        var b = 0;
        var c = 0;
        for (var i = 0; i < 4; i++) {
            var d = a.find('tr:eq(' + i + ')');
            if ($('td:eq(1)', $(d)).text().trim() !== '' && $('td:eq(1)', $(d)).text().trim() === '1') b++;
            if ($('td:eq(2)', $(d)).text().trim() !== '' && $('td:eq(2)', $(d)).text().trim() === '1') c++
        }
        $('#hasil1').text(b);
        $('#hasil2').text(c);
        $('#hasilColName').html('Hasil : <strong>(' + (b > 2 ? 'Layak' : 'Tidak Layak') + ')</strong>')
    }
    $('.tes1').on('change', function() {
        testKelayakan('.tes1', '#aa1', '#aa2')
    });
    $('.tes2').on('change', function() {
        testKelayakan('.tes2', '#bb1', '#bb2')
    });
    $('.tes3').on('change', function() {
        testKelayakan('.tes3', '#cc1', '#cc2')
    });
    $('.tes4').on('change', function() {
        testKelayakan('.tes4', '#dd1', '#dd2')
    })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<table border="1">
    <tbody>
        <tr>
            <td>Kesimpulan I<span class="red">*</span></td>
            <td>
                <select id="k1" class="tes1" name="kelayakan1" size="1">
                  <option value="">-- Pilih Salah Satu --</option>
                  <option value="1">Layak</option>
                  <option value="0">Tidak Layak</option>
                </select>
            </td>
        </tr>

        <tr>
            <td>Kesimpulan II<span class="red">*</span></td>
            <td>
                <select id="k2" class="tes2" name="kelayakan2" size="1">
                  <option value="">-- Pilih Salah Satu --</option>
                  <option value="1">Layak</option>
                  <option value="0">Tidak Layak</option>
                </select>
            </td>
        </tr>

        <tr>
            <td>Kesimpulan III<span class="red">*</span></td>
            <td>
                <select id="k3" class="tes3" name="kelayakan3" size="1">
                  <option value="">-- Pilih Salah Satu --</option>
                  <option value="1">Layak</option>
                  <option value="0">Tidak Layak</option>
                </select>
            </td>
        </tr>

        <tr>
            <td>Kesimpulan IV<span class="red">*</span></td>
            <td>
                <select id="k4" class="tes4" name="kelayakan4" size="1">
                  <option value="">-- Pilih Salah Satu --</option>
                  <option value="1">Layak</option>
                  <option value="0">Tidak Layak</option>
                </select>
            </td>
        </tr>
    </tbody>
</table>

<table style="width:100%;" id="tabel_data" border="1">
    <thead>
        <tr>
            <th colspan="5">Rekapitulasi Kelayakan</th>
        </tr>
        <tr>
            <th>Parameter</th>
            <th>Layak</th>
            <th>Tidak Layak</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td style="text-align:left;">Indeks Rumah</td>
            <td>
                <div id="aa1" style="text-align:center;"></div>
            </td>
            <td>
                <div id="aa2" style="text-align:center;"></div>
            </td>
        </tr>
        <tr>
            <td style="text-align:left;">Indeks Kepemilikan Harta</td>
            <td>
                <div id="bb1" style="text-align:center;"></div>
            </td>
            <td>
                <div id="bb2" style="text-align:center;"></div>
            </td>
        </tr>
        <tr>
            <td style="text-align:left;">Indeks Pendapatan Keluarga</td>
            <td>
                <div id="cc1" style="text-align:center;"></div>
            </td>
            <td>
                <div id="cc2" style="text-align:center;"></div>
            </td>
        </tr>
        <tr>
            <td style="text-align:left;">Indeks Pengeluaran Harta</td>
            <td>
                <div id="dd1" style="text-align:center;"></div>
            </td>
            <td>
                <div id="dd2" style="text-align:center;"></div>
            </td>
        </tr>
        <tr>
            <td align="center" id="hasilColName">Hasil</td>
            <td>
                <div id="hasil1" style="text-align:center;"></div>
            </td>
            <td>
                <div id="hasil2" style="text-align:center;"></div>
            </td>
        </tr>

    </tbody>
</table>