Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. <select class="form-control input-lg" size="1" name="tipe" id="tipe">
  2. <option value="" disabled selected>Tipe Barang</option>
  3. <option value="10000">Barang</option>
  4. <option value="11000">Motor</option>
  5. <option value="12000">Hewan</option>
  6. <option value="15000">Dokumen</option>
  7. </select>
  8.  
  9. <input type="hidden" name="toppings_tot" value="0" />
  10.  
  11. <input type="number" class="form-control" name="tarif" id="tarif" value="0" readonly="readonly" disabled required />
  12. <span id="tarif"></span>
  13.  
  14. <script type="text/javascript">
  15. // functions assigned to onchange properties
  16. document.getElementById('tipe').onchange = function() {
  17. // access form and value properties via this (keyword)
  18. var form = this.form;
  19. var tarif = parseFloat( this.value ) + parseFloat( form.elements['toppings_tot'].value );
  20. form.elements['tarif'].value = formatDecimal(tarif);
  21. };
  22.  
  23. document.getElementById('toppings').onchange = function() {
  24. var form = this.form;
  25. form.elements['toppings_tot'].value = 0; // reset toppings total
  26.  
  27. // getSelectedOptions function handles select-multiple
  28. // pass select list (this) and callback function
  29. getSelectedOptions( this, calcToppingsTotal );
  30.  
  31. // add toppings total and size price to get total
  32. var tops_tot = parseFloat( form.elements['toppings_tot'].value );
  33. var size_price = parseFloat( form.elements['tipe'].value );
  34. form.elements['tarif'].value = formatDecimal(tops_tot + size_price );
  35. };
  36.  
  37. // called for each selected option, adding its value to toppings total
  38. function calcToppingsTotal(opt) {
  39. // get reference to hidden toppings total field
  40. var tot_fld = opt.form.elements['toppings_tot'];
  41. var val = parseFloat( tot_fld.value );
  42. // add value of opt to total
  43. tot_fld.value = val + parseFloat( opt.value );
  44. }
  45.  
  46. // handles selected options in a select-multiple
  47. function getSelectedOptions(sel, fn) {
  48. var opts = [], opt;
  49.  
  50. for (var i=0, len=sel.options.length; i<len; i++) {
  51. opt = sel.options[i];
  52. if ( opt.selected ) {
  53. opts.push(opt);
  54. if (fn) {
  55. fn(opt);
  56. }
  57. }
  58. }
  59. return opts;
  60. }
  61.  
  62. // format val to n number of decimal places
  63. // modified version of Danny Goodman's (JS Bible)
  64. function formatDecimal(val, n) {
  65. n = n || 2;
  66. var str = "" + Math.round ( parseFloat(val) * Math.pow(10, n) );
  67. while (str.length <= n) {
  68. str = "0" + str;
  69. }
  70. var pt = str.length - n;
  71. return str.slice(0,pt) + "." + str.slice(pt);
  72. }</script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement