cara reset hitung otomatis jquery dengan php

gan gmn caranya bikin tombol reset dari koding berikut:


<html>
    <head>
        <title>
            Test DOM
        </title>

        <script type="text/javascript">
            var total = 0 ;

            function calculate ( checkbox )
            {
                var resultLabel = document.getElementById ( "result_label" ) ;
                var value = parseInt ( checkbox.value ) ;

                if ( checkbox.checked )
                    total += value ;
                else
                    total -= value ;

                resultLabel.innerHTML = total ;
            }
        </script>
    </head>

    <body>
        <input type="checkbox" value="1" id="checkbox1" onclick="calculate( this )">1</input><br />
        <input type="checkbox" value="2" id="checkbox2" onclick="calculate( this )">2</input><br />
        <input type="checkbox" value="3" id="checkbox3" onclick="calculate( this )">3</input><br />
        <input type="checkbox" value="4" id="checkbox4" onclick="calculate( this )">4</input><br /><br />

        Total: <label id="result_label">0</label>
<button>reset total</button>
    </body>
</html>

jadi resetnya tanpa harus melukukan refresh halaman/seperti kita menekan tombol F5 di keyboard

avatar echosk
@echosk

64 Kontribusi 1 Poin

Diperbarui 6 tahun yang lalu

3 Jawaban:




<!-- Penjelasan :

1. Tambahkan class='items' pada masing2 checkbox

   <input type="checkbox" value="1" class="items" .... </input><br />
   INGAT PADA MASING2 CHECKBOX

2. Buat fungsi reset() pada javascript

  function reset(){
    ....
  }

3. tambahkan property onclick pada button reset

   <button onclick="reset()">reset total</button>

 -->

<html>
<head>
    <title>
        Test DOM
    </title>
    <script type="text/javascript">

        var total = 0 ;

        function calculate ( checkbox )
        {
            var resultLabel = document.getElementById ( "result_label" ) ;
            var value = parseInt ( checkbox.value ) ;

            if ( checkbox.checked )
                total += value ;
            else
                total -= value ;

            resultLabel.innerHTML = total ;
        }

        function reset(){
            var items    = document.getElementsByClassName('items');
            var resultLabel = document.getElementById ( "result_label" ) ;

            for(i = 0; i < items.length; i++) {
                items[i].checked = false;
            }

            total = 0;
            resultLabel.innerHTML = total ;

        }

    </script>
</head>

<body>
    <input type="checkbox" value="1" class="items" id="checkbox1" onclick="calculate( this )">1</input><br />
    <input type="checkbox" value="2" class="items" id="checkbox2" onclick="calculate( this )">2</input><br />
    <input type="checkbox" value="3" class="items" id="checkbox3" onclick="calculate( this )">3</input><br />
    <input type="checkbox" value="4" class="items" id="checkbox4" onclick="calculate( this )">4</input><br /><br />

    Total: <label id="result_label">0</label>
    <button onclick="reset()">reset total</button>
</body>
</html>

Selamat Belajar, Happy Sharing (^_^) coto-coto-coto hehe

avatar ciloxz
@ciloxz

284 Kontribusi 139 Poin

Dipost 6 tahun yang lalu

mantab gan...berhasil

avatar echosk
@echosk

64 Kontribusi 1 Poin

Dipost 6 tahun yang lalu

Sip..Coto mana hehe kalau udah work dikasih jawaban terbaik biar nanti kalau ada yg sama pertanyaannya tinggal lihat paling atas,, :) + gk muncul di filter belum terjawab.

avatar ciloxz
@ciloxz

284 Kontribusi 139 Poin

Dipost 6 tahun yang lalu

Login untuk ikut Jawaban