Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <select class="form-control input-lg" size="1" name="tipe" id="tipe">
- <option value="" disabled selected>Tipe Barang</option>
- <option value="10000">Barang</option>
- <option value="11000">Motor</option>
- <option value="12000">Hewan</option>
- <option value="15000">Dokumen</option>
- </select>
- <input type="hidden" name="toppings_tot" value="0" />
- <input type="number" class="form-control" name="tarif" id="tarif" value="0" readonly="readonly" disabled required />
- <span id="tarif"></span>
- <script type="text/javascript">
- // functions assigned to onchange properties
- document.getElementById('tipe').onchange = function() {
- // access form and value properties via this (keyword)
- var form = this.form;
- var tarif = parseFloat( this.value ) + parseFloat( form.elements['toppings_tot'].value );
- form.elements['tarif'].value = formatDecimal(tarif);
- };
- document.getElementById('toppings').onchange = function() {
- var form = this.form;
- form.elements['toppings_tot'].value = 0; // reset toppings total
- // getSelectedOptions function handles select-multiple
- // pass select list (this) and callback function
- getSelectedOptions( this, calcToppingsTotal );
- // add toppings total and size price to get total
- var tops_tot = parseFloat( form.elements['toppings_tot'].value );
- var size_price = parseFloat( form.elements['tipe'].value );
- form.elements['tarif'].value = formatDecimal(tops_tot + size_price );
- };
- // called for each selected option, adding its value to toppings total
- function calcToppingsTotal(opt) {
- // get reference to hidden toppings total field
- var tot_fld = opt.form.elements['toppings_tot'];
- var val = parseFloat( tot_fld.value );
- // add value of opt to total
- tot_fld.value = val + parseFloat( opt.value );
- }
- // handles selected options in a select-multiple
- function getSelectedOptions(sel, fn) {
- var opts = [], opt;
- for (var i=0, len=sel.options.length; i<len; i++) {
- opt = sel.options[i];
- if ( opt.selected ) {
- opts.push(opt);
- if (fn) {
- fn(opt);
- }
- }
- }
- return opts;
- }
- // format val to n number of decimal places
- // modified version of Danny Goodman's (JS Bible)
- function formatDecimal(val, n) {
- n = n || 2;
- var str = "" + Math.round ( parseFloat(val) * Math.pow(10, n) );
- while (str.length <= n) {
- str = "0" + str;
- }
- var pt = str.length - n;
- return str.slice(0,pt) + "." + str.slice(pt);
- }</script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement