Skip to content

Instantly share code, notes, and snippets.

@shouhei
Created June 19, 2014 13:29
Show Gist options
  • Select an option

  • Save shouhei/90520e65171d739a82c4 to your computer and use it in GitHub Desktop.

Select an option

Save shouhei/90520e65171d739a82c4 to your computer and use it in GitHub Desktop.
calc.js
var payment_type = <?php json_encode(array('hogehoge'=>array('payment'=>'金額'))); ?>;
$('select[name="product_name"]').on('change',function(){
change_tax();
$('#price').val(get_price()).change();
});
var get_tax = function(){
return $('input[name=taxation]:checked').val() == 0?0:0.08;
}
var change_tax = function(){
if(payment_type[get_product_label()]['tax']){
return $('input[name=taxation]').val(['1']);
}
return $('input[name=taxation]').val(['0']);
}
var change_price = function(tax){
$('#tax').val(get_price() * tax);
$('#amount').val(get_price() * (1 + tax));
}
var get_price = function(){
return payment_type[get_product_label()]['payment'];
}
var get_product_label = function(){
return $('select[name="product_name"]').val();
}
var calc_tax = function(num){
return num * get_tax();
}
var calc_amount = function(num){
return num * (get_tax() + 1);
}
$('input[name=price]').on('change',function(){
$('#tax').val(calc_tax($(this).val()));
$('#amount').val(calc_amount($(this).val()));
});
$('input[name=taxation]:radio').on('change',function(){
change_price(get_tax());
});
$(function(){
$('select[name="product_name"]').change();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment