表格格式&文字添加了HTML

时间:2015-10-15 17:03:50

标签: javascript html

我想在我的HTML代码上添加表格 但我想把它作为数字格式的 例如,我希望它格式化为:Rp。 100.000)
所以" Rp。"需要在单元格的开头自动添加,数字格式如上所述.. 抱歉我的英语不好,这是我的代码,谢谢你的时间:

     <!DOCTYPE html>
<html>
<body>

<title>Preventive Maintenance Analyzer Tools</title>

<div id="container" style="width:1350px;height:600px">
<div id="header" style="background-color:#094AB2;width:1350px;height:50px">
<h1 style="font-family:verdana;color : white;">Preventive Maintenance Analyzer Tools</font></h1></div></div>

<div id="menu" style="background-color:#DAE0DA;height:560px;width:150px;float:left;">
<img src = "taxi.png" width = "130" height = "130" align = "center"><br><br><br><br><br>
<font size="4"><a href = "http://localhost/home.php">HOME</a><br><br></font>
<font size="4"><a href = "http://localhost/entry.html">CREATE NEW</a><br><br></font>
<font size="4"><a href = "">SETTINGS</a></font><br><br><br><br><br></br>
<img src = "taxi2.png" width = "140" height = "130" align = "center">
</div>

<h2><center>Silahkan Isi Detail Form Dibawah Ini</center></h2>

<div align = "center">
<fieldset>
<legend><b>Detail Onsite</b></legend>

Dari :
<select name="dari">
<option value="co">Cyber Office</option>    
</select><br><br>`

Tujuan PM :
<select id="tujuan" name="tujuan">
<option value="abb">ABB Sakti Industri</option>
<option value="ace">ACE Life Assurance</option>
<option value="cigna">Asuransi Cigna</option>
<option value="bahana">Bahana Securities</option>
<option value="cimb">Bank CIMB</option>
<option value="dki">Bank DKI</option>
<option value="icbc">Bank ICBC Indonesia</option>
<option value="bni">Bank BNI</option>
<option value="courts">Courts Retail Indonesia</option>
<option value="ffi">Frisian Flag Indonesia</option>
<option value="gel">Great Eastern Life Indonesia</option>
<option value="komatsu">Komatsu Indonesia</option>
<option value="pertamina">Pertamina EP</option>
<option value="rscm">Rumah Sakit Cipto Mangunkusomo (RSCM)</option>
<option value="siloam">Siloam Hospital</option>
<option value="takeda">Takeda Indonesia</option>
<option value="vni">Visionet</option>
</select><br><br>

Cuaca : 
<select id="cuaca" name="cuaca">
<option value="normal">Normal / Cerah</option>
<option value="gerimis">Gerimis</option>
<option value="hujan">Hujan</option>
</select><br><br>

Faktor Eksternal (contoh demo, banjir, macet luar biasa dll) : <br>
<select id="fe" name="fe">
<option value="false">Tidak Ada</option>
<option value="true">Ada</option>
</select><br><br>

<a href="javascript:void(0)" id="submit">Submit</a>

</form>
</div>


<div id="output" style="display:none">


      <center><h2>Estimasi Claim</h2></center>
      <table border="4" ALIGN="center" >
      <tr>
        <th>Keterangan</th>
        <th>Estimasi</th>
       </tr>
        <td> Estimasi Argo Taxi </td>
        <td align="right" id="output_t1"></td>
        <tr>
        <td> Cuaca </td>
        <td align="right" id="output_t2"></td>
        <tr>
        <td> Faktor Eksternal </td>
        <td align="right" id="output_t3"></td>
        <tr>
        <td> Total Claim </td>
        <td align="right" span class="price" id="output_t4"></td>
        <tr>
        </table>

        <br><center><b>Selamat Atas Kemenangan Anda, Sampai Jumpa Pada Kemenangan Berikutnya</b></center>

 </div>

<script>
document.getElementById('submit').onclick = function(){

  document.getElementById('output').style.display = "block";

  var tujuan        = document.getElementById('tujuan').value;
  var cuaca         = document.getElementById('cuaca').value;
  var fe            = document.getElementById('fe').value;

  var t1; 
  var t2; 
  var t3;
  var t4;

  switch(tujuan){

    case "abb":
      t1 = 370000;
      break;

    case "ace":
      t1 = 300000;
      break;

    case "cigna":
      t1 = 170000;
      break;

    case "bahana":
      t1 = 375000;
      break;

    case "cimb":
      t1 = 300000;
      break;

    case "dki":
      t1 = 300000;
      break;

    case "icbc":
      t1 = 330000;
      break;

    case "bni":
      t1 = 350000;
      break;  

     case "courts":
      t1 = 350000;
      break;

    case "ffi":
      t1 = 430000;
      break;

    case "gel":
      t1 = 360000;
      break;

     case "komatsu":
      t1 = 580000;
      break;

    case "pertamina":
      t1 = 370000;
      break;

    case "rscm":
      t1 = 370000;
      break;

    case "siloam":
      t1 = 50000;
      break;

    case "takeda":
      t1 = 350000;
      break;

    case "visionet":
      t1 = 20000;
      break;

  } 

  switch(cuaca){

    case "normal":
      t2 = 0;
      break;

    case "gerimis":
      t2 = 20000;
      break;

    case "hujan":
      t2 = 40000;
      break;

    }

   if(fe == 'true'){
    t3    = 40000;
  }
  else{
    t3    = 0;
  }
  t4 = t1 + t2 + t3;
  console.log(t4);

  document.getElementById('output_t1').innerHTML = t1; 
  document.getElementById('output_t2').innerHTML = t2;
  document.getElementById('output_t3').innerHTML = t3;   
  document.getElementById('output_t4').innerHTML = t4;
}
</script>

</body>

<div id="footer" style="background-color:#094AB2;clear:both;text-align:center;">
<font color = "white">Copyright : M. Rinaldy Aulia 2015</font></div>
</html>

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你要确保你的号码不是“370000”,而应该是“Rp.370 000”,或者“45000”应该是“Rp.45 000”

如果这是正确的,请使用将格式化数字的功能。我在下面创建了一个名为rp_beautifier的函数,并且还包含了一些关于如何实现它的示例代码。希望我理解正确,这有帮助!

这是功能(在第3行,我们添加“Rp。”以及$ 1后的空格。如果是USD,则为“$ 1”,而不是“$ 1”)

function rp_beautifier(someNumber){
    someNumber = parseFloat(someNumber).toFixed(0);
    someNumber = "Rp. " + String(someNumber).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1 ")
    return someNumber;
}

以下是你可以称之为的方式

document.getElementById('output_t1').innerHTML = rp_beautifier(t1); 
document.getElementById('output_t2').innerHTML = rp_beautifier(t2);
document.getElementById('output_t3').innerHTML = rp_beautifier(t3);   
document.getElementById('output_t4').innerHTML = rp_beautifier(t4);