简体   繁体   English

添加div和输入框

[英]adding div and input box

I would like to be able to do a quick calculation where I add the contents of a div and input. 我希望能够进行快速计算,在其中添加div的内容并输入。 I can do this with 2 inputs but can't figure out how to do it this way. 我可以用2个输入来做到这一点,但无法弄清楚如何用这种方式。 My attempts at coding have got me here: 我的编码尝试使我来到这里:

 var one = document.getElementById('one'), two = document.getElementById('calculate'), total = document.getElementById('total'); document.getElementById('calculate').onchange = function() { total.innerHTML = parseInt(one.innerHTML) + parseInt(two.innerHMTL); }; 
 <div id="one">11</div> <input type=number value=2 id="calculate"> <div id="total">0</div> 

I tried changing "parseInt(two.innerHTML)" to "parseInt(two)" but it was unsuccessful. 我尝试将“ parseInt(two.innerHTML)”更改为“ parseInt(two)”,但未成功。 I get the NotANumber/NaN error. 我收到NotANumber / NaN错误。 Any help would be greatly appreciated. 任何帮助将不胜感激。

since two in you case is an input tag it doesn't have the innerHTML . 因为在您的情况下, twoinput标签,所以它没有innerHTML Use value attribute instead. 请使用value属性。 so it'll look like this: 所以看起来像这样:

document.getElementById('calculate').onchange = function() {
    total.innerHTML = parseInt(one.innerHTML) + parseInt(two.value);
};

To get value from a input tag use .value document.getElementById('input').value 要从输入标签获取值,请使用.value document.getElementById('input').value

So in your case try parseInt(two.value) 因此,在您的情况下,请尝试parseInt(two.value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM