如何使用Javascript获得结果 [英] How to Get the Result Using Javascript

查看:36
本文介绍了如何使用Javascript获得结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码,并希望在 intTotal 中获取结果.但这并没有给我我想要的结果.

I am working with this code and would like to get the result inside intTotal. But it doesn't give me the result I wish.

window.onload = function() {
  var intNum = document.getElementById("intNum");
  for (var i = 1; i <= 3000; i++) {
    var option = document.createElement("option");
    option.innerHTML = i;
    option.value = i;
    intNum.appendChild(option);
  }
};

function jml_total() {
  var hrg_sat = parseInt(document.querySelector("hrg_sat").innerHTML);
  var jml_pilih = parseInt(document.getElementById("intNum").value);
  var int_hitung = hrg_sat * jml_pilih;
  document.getElementById("intTotal").value = int_hitung;
}

<div id="hrg_sat">3000</div>
<select id="intNum" onChange="jml_total(this.value)">
  <option value="" disabled selected>Pilih Jumlah ... </option>
</select>
<br />
<div id="intTotal"></div>

我尝试了线程.或者,您拥有最简单的一个,但不使用jquery.

I tried this thread. Or, you have the simplest one, but not using jquery.

推荐答案

您需要使用#作为前缀

var hrg_sat = parseInt(document.querySelector("#hrg_sat").innerHTML);

此外,正如前面的答案所提到的,您需要使用 innerHTML 属性而不是 value 来在< div> 元素

Also, as the previous answer has mentioned, you need to use innerHTML property instead of value to display the text on the DOM on a <div> element

window.onload = function() {
  var intNum = document.getElementById("intNum");
  for (var i = 1; i <= 3000; i++) {
    var option = document.createElement("option");
    option.innerHTML = i;
    option.value = i;
    intNum.appendChild(option);
  }
};

function jml_total() {
  var hrg_sat = parseInt(document.querySelector("#hrg_sat").innerHTML);
  var jml_pilih = parseInt(document.getElementById("intNum").value);
  var int_hitung = hrg_sat * jml_pilih;
  document.getElementById("intTotal").innerHTML = int_hitung;
}

<div id="hrg_sat">3000</div>
<select id="intNum" onChange="jml_total(this.value)">
  <option value="" disabled selected>Pilih Jumlah ... </option>
</select>
<br />
<div id="intTotal"></div>

这篇关于如何使用Javascript获得结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆