简体   繁体   中英

How to calculate total in a table using javascript

I have tried to do the calculation of the marks obtained and want it to display in a table. As in the javascript, the calculation that i have did is as below:

<script>
                        function kira1() {
                            var myeBook = document.getElementById('eBook').value;
                            var result = document.getElementById('result1');
                            var myResult = myeBook * 3;
                            result.value = myResult;
                        }
                        function kira2() {
                            var mybukuTeks = document.getElementById('bukuTeks').value;
                            var result = document.getElementById('result2');
                            var myResult = mybukuTeks * 3;
                            result.value = myResult;
                        }
                        function kira3() {
                            var mymodul = document.getElementById('modul').value;
                            var result = document.getElementById('result3');
                            var myResult = mymodul * 2;
                            result.value = myResult;
                        }
                        function kira4() {
                            var myjurnal = document.getElementById('jurnal').value;
                            var result = document.getElementById('result4');
                            var myResult = myjurnal * 2;
                            result.value = myResult;
                        }
                        function kira5() {
                            var myePembelajaran = document.getElementById('ePembelajaran').value;
                            var result = document.getElementById('result5');
                            var myResult = myePembelajaran * 1;
                            result.value = myResult;
                        }
                        function kira6() {
                            var myinovasi = document.getElementById('inovasi').value;
                            var result = document.getElementById('result6');
                            var myResult = myinovasi * 1;
                            result.value = myResult;
                        }
                        function kira7() {
                            var a = document.getElementById('result1').value;
                            var b = document.getElementById('result2').value;
                            var c = document.getElementById('result3').value;
                            var d = document.getElementById('result4').value;
                            var e = document.getElementById('result5').value;
                            var f = document.getElementById('result6').value;
                            var result = document.getElementById('jumlah_1A');
                            var myResult = a + b + c + d + e + f;
                            result.value = myResult;
                        }           
                    </script>

But when i tried to run it, the total is not displaying as i want it to.

This is the output: 在此处输入图片说明

The total should be display at the "Jumlah Skor 1A"

Try using:

document.getElementById('jumlah_1A').innerHTML
i.e. document.getelementbyid('idname').innerHTML

As per my understanding 'jumlah_1A' is a div not a Input box. so use .innerHTML instead of .value

W3school Link: http://www.w3schools.com/jsref/prop_html_innerhtml.asp

var a = parseInt(document.getElementById('result1').value);
var b = parseInt(document.getElementById('result2').value);
var c = parseInt(document.getElementById('result3').value);
var d = parseInt(document.getElementById('result4').value);
var e = parseInt(document.getElementById('result5').value);
var f = parseInt(document.getElementById('result6').value);

Convert all to integer before adding the values. if jumlah_1A element is not a textbox then do

result.innerHTML = myResult;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM