Using Forms in HTML to calculate total price

后端 未结 1 1969
深忆病人
深忆病人 2021-01-24 15:27

How can I write a function that will calculate to total price of the computer components selected by the user, this is what I have so far but now I seem to be stuck. Any ideas?

1条回答
  •  被撕碎了的回忆
    2021-01-24 15:37

    • Add id:s to select-elements
    • Add value attribute to option tag
    • Fix javascript

    Something like this:

      function calculatePrice(){
    
          //Get selected data  
          var elt = document.getElementById("memoryItem");
          var memory = elt.options[elt.selectedIndex].value;
    
          elt = document.getElementById("hddItem");
          var hdd = elt.options[elt.selectedIndex].value;
    
          elt = document.getElementById("networkItem");
          var network = elt.options[elt.selectedIndex].value;
    
          //convert data to integers
          memory = parseInt(memory);
          hdd = parseInt(hdd);
          network = parseInt(network);
    
          //calculate total value  
          var total = memory+hdd+network; 
    
          //print value to  PicExtPrice 
          document.getElementById("PicExtPrice").value=total;
    
     }
    

    And html

    The new calculated price:

    Try it here http://jsfiddle.net/Wm6zC/

    0 讨论(0)
提交回复
热议问题