Friday, 2 March 2018

Perhitungan Angka Tanpa klik Submit


<script src="http://code.jquery.com/jquery-2.1.4.js"></script>
<script type="text/javascript">
  $(document).ready(function(){
      $("#diskon").keyup(function(){
        var harga  = Number($("#harga").val().replace(/\./g,""));
        var diskon = Number($("#diskon").val().replace(/\./g,""));
        var total  = Number(((diskon-harga)/harga)*100).toFixed(2);
        $("#totBayar").val(total +' %');
      });
  });
</script>

<table border="0" cellpadding="5" align="center" bgcolor="#00ff00">
  <form action="" method="post">
     <tr>
        <td>Harga awal:</td>
        <td><input type="text" id="harga" onkeydown="return numbersonly(this, event);" onkeyup="javascript:tandaPemisahTitik(this);"></td>
     </tr>
     <tr>
        <td>Harga perubahan :</td>
        <td><input type="text" id="diskon" onkeydown="return numbersonly(this, event);" onkeyup="javascript:tandaPemisahTitik(this);"></td>
     </tr>
     <tr>
        <td>Persentase perubahan Harga :</td>
        <td><input type="text" id="totBayar" disabled /></td>
     </tr>  </form>
</table>
<script>
 function tandaPemisahTitik(b){
 var _minus = false;
 if (b<0) _minus = true;
 b = b.toString();
 b=b.replace(".","");
 b=b.replace("-","");
 c = "";
 panjang = b.length;
 j = 0;
 for (i = panjang; i > 0; i--){
 j = j + 1;
 if (((j % 3) == 1) && (j != 1)){
 c = b.substr(i-1,1) + "." + c;
 } else {
 c = b.substr(i-1,1) + c;
 }
 }
 if (_minus) c = "-" + c ;
 return c;
 }
 function numbersonly(ini, e){
 if (e.keyCode>=48){
 if(e.keyCode<=57){
 a = ini.value.toString().replace(".","");
 b = a.replace(/[^\d]/g,"");
 b = (b=="0")?String.fromCharCode(e.keyCode):b + String.fromCharCode(e.keyCode);
 ini.value = tandaPemisahTitik(b);
 return false;
 }
 else if(e.keyCode<=105){
 if(e.keyCode>=96){
 //e.keycode = e.keycode - 47;
 a = ini.value.toString().replace(".","");
 b = a.replace(/[^\d]/g,"");
 b = (b=="0")?String.fromCharCode(e.keyCode-48):b + String.fromCharCode(e.keyCode-48);
 ini.value = tandaPemisahTitik(b);
 //alert(e.keycode);
 return false;
 }
 else {return false;}
 }
 else {
 return false; }
 }else if (e.keyCode==48){
 a = ini.value.replace(".","") + String.fromCharCode(e.keyCode);
 b = a.replace(/[^\d]/g,"");
 if (parseFloat(b)!=0){
 ini.value = tandaPemisahTitik(b);
 return false;
 } else {
 return false;
 }
 }else if (e.keyCode==95){
 a = ini.value.replace(".","") + String.fromCharCode(e.keyCode-48);
 b = a.replace(/[^\d]/g,"");
 if (parseFloat(b)!=0){
 ini.value = tandaPemisahTitik(b);
 return false;
 } else {
 return false;
 }
 }else if (e.keyCode==8 || e.keycode==46){
 a = ini.value.replace(".","");
 b = a.replace(/[^\d]/g,"");
 b = b.substr(0,b.length -1);
 if (tandaPemisahTitik(b)!=""){
 ini.value = tandaPemisahTitik(b);
 } else {
 ini.value = "";
 }
 return false;
 } else if (e.keyCode==9){
 return true;
 } else if (e.keyCode==17){
 return true;
 } else {
 //alert (e.keyCode);
 return false;
 }
 }
</script>

No comments:

Post a Comment