lundi 5 octobre 2015

jQuery sum of checkbox and textbox value

jQuery sum of checkbox and textbox value

Get sum of checkbox value on click event is working fine. But I want changes in result accordingly. I change textbox value.

Example here : http://ift.tt/1JNW1cT

enter image description here

<table border="1">
  <tr>
    <td>10
      <input type="checkbox" class="tot_amount" value="10" id="filled-in-box1" onclick="chaked1();">
      <input id="one" type="text">
    </td>
  </tr>
  <tr>
    <td>20
      <input type="checkbox" class="tot_amount" value="20" id="filled-in-box2" onclick="chaked2();">
      <input id="two" type="text">
    </td>
  </tr>
  <tr>
    <td>Total
      <input type="text" id="total1" readonly>
    </td>
  </tr>
</table>

JS

    <script>
    function chaked1(){
    $("#filled-in-box1").click(function(event)
    {
    if(this.checked)
    {
    document.getElementById("one").value=document.getElementById("filled-in-box1").value;
    document.getElementById('one').readOnly = false;
    }
    else
    {
    document.getElementById("one").value="0";
    document.getElementById('one').readOnly = true;
    }

    });
    }
    </script>

    <script>
    function chaked2(){
    $("#filled-in-box2").click(function(event)
    {
    if(this.checked)
    {
    document.getElementById("two").value=document.getElementById("filled-in-box2").value;
    document.getElementById('two').readOnly = false;
    }
    else
    {
    document.getElementById("two").value="0";
    document.getElementById('two').readOnly = true;
    }
    });

    }
    </script>


    <script>
    $(".tot_amount").click(function(event) {
    var total = 0;
    $(".tot_amount:checked").each(function() 
    {
    total += parseInt($(this).val());
    });

    if (total == 0) {
    $('#total1').val('');
    }
    else {
    $('#total1').val(total);
    }
    });
    </script>




Aucun commentaire:

Enregistrer un commentaire