这是我的代码

<html>
<head>
    <title>
        Aplikasi KRS
    </title>
</head>
<body>
<p>IPK : 1.xx</p>
    <form name="formTes" method="post">
        <script>
            function cekKrs()
            {
                var jum_sks = 0;
                for(var i=0; i<7; i++)
                {
                    if(document.formTes.elements[i].checked)
                        jum_sks += mataKuliah;
                }


                document.formTes.fieldJumlah.value = jum_sks;

                if(jum_sks > 15)
                {
                    alert("Anda harus mengurangi SKS\n karena melebihi 15 SKS");
                }

            }

            function cekKirim()
            {
                if(document.formTes.fieldJumlah.value > 15)
                    alert("Anda harus mengurangi SKS\n karena melebihi 15 SKS");
                else
                    window.location.href = "tesform.html";
            }
        </script>

        <table border="2">
        <tr><th>Kode</th><th>Mata Kuliah</th><th>SKS</th><th><Pilihan/th></tr>
        <?php
            include "koneksi.php";
            $sql = "SELECT * FROM mata_kul";
            $query = $con->query($sql);
            while($mataKuliah = $query->fetch_array())
            {
                echo "<tr><td>". $mataKuliah['kodeMK'] ."</td>";
                echo "<td>". $mataKuliah['namaMK'] ."</td>";
                echo "<td>". $mataKuliah['sks'] ."</td>";
                echo "<script>var mataKuliah= ".$mataKuliah['sks']."</script>";
                echo "<td><input name='mk". $mataKuliah['kodeMK'] ."' type='checkbox' onclick='cekKrs()'></td>";
            }
        ?>
        <tr><td colspan="2" align="right">Jumlah SKS</td>
        <td colspan="2"><input size="3" value="0" name="fieldJumlah" type="text" readonly="readonly"></td></tr></table>
        <br>
        <input type="button" name="tombolKirim" value="Kirim" onclick="cekKirim()">
    </form>
</body>
</html>


我的问题是

“为什么当我单击清单时,它显示jumlah SKS中的sks的最后一个值,而不是我单击的sks检查值?例如当我单击清单中值为2的sks时,应在jumlah SKS中显示2而不是8”

这是图片

你能告诉我我哪里出问题了吗?我还很新

最佳答案

我猜您需要将复选框obj作为参数发送给cekKrs(),然后在函数cekKrs中评估obj是否被选中为+或-总计中的obj.value。

试试这个DEMO

HTML(草稿)

<table>
<!-- while($mataKuliah = $query->fetch_array())
        { -->
    <tr>
      <!-- echo "<td><input name='mk". $mataKuliah['kodeMK'] ."' type='checkbox' onclick='cekKrs(this)' value='".$mataKuliah['sks']."'></td>";    // PHP  -->
       <td><input type='checkbox' onclick='cekKrs(this)' value='1'>1</td>
    </tr>
    <tr>
       <td><input type='checkbox' onclick='cekKrs(this)' value='2'>2</td>
    </tr>
    <tr>
       <td><input type='checkbox' onclick='cekKrs(this)' value='3'>3</td>
    </tr>
    <!-- } -->
</table>
<input size="3" value="0" name="fieldJumlah" id='fieldJumlah' type="text" readonly="readonly">


onclick ='cekKrs(this)'发送复选框obj,并将mataKuliah的值保留在复选框值内

JS

 function cekKrs(chkObj)
            {
                //Get current total
                var jum_sks = parseInt(document.getElementById('fieldJumlah').value);
                //Get checkbox value clicked
                var mataKuliah = parseInt(chkObj.value);

                    //Evaluate check to + or -
                    if(!chkObj.checked)
                    {
                        mataKuliah = (mataKuliah * -1)
                    }

                    jum_sks += mataKuliah;

                 //Set new total
                 document.getElementById('fieldJumlah').value = jum_sks;


            }

07-28 11:45