TypeError:document.getElementById(...)很简单,但请helpppp

时间:2015-10-15 15:22:11

标签: javascript html

我正在创建一个简单的程序,实际上我是网络工程师而不是程序员:) 我正在为小组织创建这个HTML + Javascript(不是为了商业) 这是错误代码:TypeError:document.getElementById(...)为null 这是我的源代码,请helpp:

  <!DOCTYPE html>
<html>
<body>

<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 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>
<option value="LK">Lippo Kuningan</option>
</select><br><br>
Tujuan PM :
<select 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 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 name="fe">
<option value="tidak">Tidak Ada</option>
<option value="ada">Ada</option>
</select><br><br>

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

</form>
</div>


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


      <center><h2>Tarif</h2></center>
      <table border="4" ALIGN="center" >
        <td> Tarif </td>
        <td id="output_t4"></td>
        <tr>
        <td> Sub Total </td>
        <td id="output_t2"></td>
        <tr>

        </table>

 </div>

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

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

  var dari          = document.getElementById('dari').value;
  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":
      t3 = 40000;
      break;

    }

    switch(fe){

    case "tidak":
      t3 = 0;
      break;

    case "ada":
      t3 = 30000;
      break;

    }   

  t4 = t1 + t2 + t3;
  console.log(t4);

  document.getElementById('output_t2').innerHTML = t2; 
  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</font></div>
</html>

1 个答案:

答案 0 :(得分:1)

要使用JavaScript的document.getElementById();访问表单元素的值,您需要为每个select元素添加ID。

目前,您似乎对ID的“名称”感到困惑。

要解决此问题,我会为每个select元素添加“id”属性。

例如,

<select id="fe" name="fe">...

您可以在javascript中使用id获取任何元素的值:

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