dimanche 1 mai 2016

Javascript IF statement not working consistently

I'm trying to create an if statement which triggers different functions for different levels of numerical form inputs. The code below ( http://ift.tt/1Ne8wr3 ) should should trigger GetTotalHigh() if the input > 100 and GetTotalLow() if the input <= 100 but doesn't seem to be working (i.e. it seems to triggers one of the functions at random). Any help much appreciated. Mike

<head>
  <script src="http://ift.tt/1ROJ5xx"></script>
  <script src="http://ift.tt/1RY4jZJ"></script>
  <script src="http://ift.tt/1XSXV5s"></script>
  <link href="http://ift.tt/1jAc5cP" rel="stylesheet" />
</head>

TOTAL:
<div id="chkTotal"></div>
<br>
<input type="text" name="input1" id="input1" />
<input type="button" value="Change X" onclick="Confirm(this)" />
<br>
<br>
<input type="text" name="input2" id="input2" />
<input type="button" value="Change Y" onclick="Confirm(this)" />

<script>

var input1 = 5555;
var input2 = 666;

$(document).ready(function() {
  document.getElementById("input1").value = input1;
  document.getElementById("input2").value = input2;
  GetFirstTotal();
});

function GetFirstTotal() {
  var total = 0;
  $('input[type=text]').each(function(index, value) {
    total += parseInt($(value).val() || 0);
  });

  $("#chkTotal").html(total);
}

function Confirm() {
  input = parseInt($('#input1').val() || 0, 10);
  input = parseInt($('#input2').val() || 0, 10);
  if (input <= 100) {
    GetTotalLow()
  };
  if (input > 100) {
    GetTotalHigh()
  };
}


function GetTotalHigh() {

  total = 0
  BootstrapDialog.confirm('High - are you sure you want to do this?', function(result) {
    if (result) {
      input1 = parseInt($('#input1').val() || 0, 10);
      input2 = parseInt($('#input2').val() || 0, 10);

      total = input1 + input2;

      $("#chkTotal").html(total);
    } else {
      document.getElementById("input1").value = input1;
      document.getElementById("input2").value = input2;
    }
  });
}

function GetTotalLow() {

  total = 0
  BootstrapDialog.confirm('Low - are you sure you want to do this?', function(result) {
    if (result) {
      input1 = parseInt($('#input1').val() || 0, 10);
      input2 = parseInt($('#input2').val() || 0, 10);

      total = input1 + input2;

      $("#chkTotal").html(total);
    } else {
      document.getElementById("input1").value = input1;
      document.getElementById("input2").value = input2;
    }
  });
}
</script>

Aucun commentaire:

Enregistrer un commentaire