如何使用javascript

时间:2016-05-16 08:18:31

标签: javascript jquery-calculation

我试图计算所获得的标记,并希望它显示在表格中。和javascript一样,我所做的计算如下:

<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>

但是当我试图运行它时,总数不会显示为我想要的。

这是输出: enter image description here

总数应显示在&#34; Jumlah Skor 1A&#34;

2 个答案:

答案 0 :(得分:0)

尝试使用:

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

按照我的理解&#39; jumlah_1A&#39;是一个div而不是输入框。所以使用.innerHTML而不是.value

W3school链接:     http://www.w3schools.com/jsref/prop_html_innerhtml.asp

答案 1 :(得分:0)

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);

在添加值之前将all转换为整数。 如果jumlah_1A元素不是文本框,那么

result.innerHTML = myResult;