Javascript - push multiple values

时间:2017-08-04 12:37:15

标签: javascript push

I have had a bit of Javascript working with 2 variable, but I need to extend it to 3:

<script type="text/javascript">

var SoftwareDetails = [

  ['**Select','0','0'],
  ['1 License (999)','999','1'],
  ['2 - 4 Licenses (949 each)','949','2'],
  ['5 - 9 Licenses (899 each)','899','5'],
  ['10 - 19 Licenses (799 each)','799','10'],
  ['20 - 29 Licenses (699 each)','699','20'],
  ['30 - 49 Licenses (599 each)','599','30'],
  ['50 + Licenses (499 each)','499','50']
];
function SetupSoftware(TA) {
  var str = "<select id='Software' onchange='SoftwareInfo()' style='font-weight:bold;'>";
  for (var i=0; i<SoftwareDetails.length; i++) {
    str += '<option value="'+SoftwareDetails[i].join('|')+'">'+SoftwareDetails[i][0]+'</option>';
  }
  str +='</select>';
  document.write(str);

}

    $(function () {
        $("#Software").change(function () {
            if ($(this).val() == "50 + Licenses (499 each)|499|50") {
                document.getElementById("quantity").value = 50;
            } else if ($(this).val() == "30 - 49 Licenses (599 each)|599|30") {
                document.getElementById("quantity").value = 30;
            } else if ($(this).val() == "20 - 29 Licenses (699 each)|699|20") {
                document.getElementById("quantity").value = 20;
            } else if ($(this).val() == "10 - 19 Licenses (799 each)|799|10") {
                document.getElementById("quantity").value = 10;
            } else if ($(this).val() == "5 - 9 Licenses (899 each)|899|5") {
                document.getElementById("quantity").value = 5;
            } else if ($(this).val() == "2 - 4 Licenses (949 each)|949|2") {
                document.getElementById("quantity").value = 2;
            } else if ($(this).val() == "1 License (999)|999|1") {
                document.getElementById("quantity").value = 1;
            } else {
                document.getElementById("quantity").value = 1;
            }
        });
    });

function SoftwareInfo() {
  var sel = document.getElementById('Software').selectedIndex;
  var tmp = [];  tmp.push(sel);
  for (var i=1; i<9; i++) { tmp.push(SoftwareDetails[sel][i]); }
  document.getElementById('SoftwareTxtVal').value = tmp[1];
  document.getElementById("softwareEachdisplay").value = tmp[1];

    var costOfLicenses = tmp[1]; 
    var numberOfLicenses = tmp[2];
    var result = document.getElementById('softwareTotaldisplay'); 
    var myResult = costOfLicenses * numberOfLicenses;
    result.value = myResult;

}
</script>

The first bit sets up the var SoftwareDetails (where I have added my third variable in each string)

Function SetupSoftware(TA) creates my 'select' dropdown and the following function and sets the quantity

The next function SoftwareInfo is where I'm having my trouble...

The first part works fine and pushes tmp[1] to my textbox 'softwareEachdisplay', but the last part of the function isn't working... I can't seem to 'push' out the third variable to create my TOTAL which I thought I could do by multiplying tmp[1] * tmp[2]...

This sum is always 1 step behind, ie it is taking the previously selected quantiy and multiplying * tmp[1]

Where am I going wrong? Why can't I get tmp[2] to push out? It's probably staring me right in the face, but I just can't see it...

If you can help me out and point me in the right direction, I'd be very grateful

Thanks

0 个答案:

没有答案