显示NaN的Javascript变量

时间:2015-12-08 14:40:08

标签: javascript parsing variables numbers integer

有人可以建议我如何让我的最终变量显示为数字(我的函数变量的值)而不是NaN?

<form id="computeroi">
                <fieldset>
                    <div class="col-1 pblue"> <p class="float-left"> 1. How many monthly visitors does your website get per month? </p>  <input type="number" value="0" id="monthlyvisitors" class="width-50" onchange="computeroi()"> </div>
                    <div class="col-1 pblue"> <p class="float-left"> 2. How many of those visitors are from a mobile device? </p>  <input type="number" value="0" id="mobilevisitors" class="width-50" onchange="computeroi()"> </div>
                    <div class="col-1 pblue"> <p class="float-left"> 3. What is your average deal worth ? </p>  <input type="number" value="0" id="dealworth" class="width-50" onchange="computeroi()"> </div>
                    <div class="col-1 pblue"> <input type="submit" value="Submit" class="width-25" id="submit" width="25"> </div>
                    <div class="col-1 "> <h1 id="newdealw"> </h1> </div>
                </fieldset>
            </form>

function computeroi() {
var monthlyv = document.getElementById('monthlyvisitors').value;
var mobilet = document.getElementById('mobilevisitors').value;
var dealw = document.getElementById('dealworth').value;

var newdeals = (mobilevisitors / monthlyvisitors); 
var newdealw = (newdeals * dealworth);

document.getElementById('newdealw').innerHTML = newdealw;

}

4 个答案:

答案 0 :(得分:1)

mobilevisitorsmonthlyvisitorsdealworth未在您的方法中的任何位置声明或定义。

而是将ids monthlyvisitorsmobilevisitorsdealworth的值分配到monthlyvmobiletdealw

var monthlyv = document.getElementById('monthlyvisitors').value;
var mobilet = document.getElementById('mobilevisitors').value;
var dealw = document.getElementById('dealworth').value;

试试这个

var monthlyv = document.getElementById('monthlyvisitors').value;
var mobilet = document.getElementById('mobilevisitors').value;
var dealw = document.getElementById('dealworth').value;

var newdeals = ( +mobilet / +monthlyv ); 
var newdealw = (newdeals * +dealw );
变量前的

N:b: +符号会将其视为数字。

答案 1 :(得分:0)

var monthlyv = document.getElementById('monthlyvisitors').value;
var mobilet = document.getElementById('mobilevisitors').value;
var dealw = document.getElementById('dealworth').value;

var newdeals = (mobilevisitors / monthlyvisitors); 
var newdealw = (newdeals * dealworth);

代码中没有mobilevisitorsmonthlyvisitors个变量。

在执行数学运算之前也要相应地使用parseInt()parseFloat()

答案 2 :(得分:0)

您的代码中没有mobilevisitors,monthlyvisitors变量。 juste改变了最后两行:

var newdeals = (mobilet / monthlyv); 
var newdealw = (newdeals * dealw);

答案 3 :(得分:0)

您尚未声明变量 mobilevisitors 或此 monthlyvisitors ,因此您无法获得价值。你应该使用变量monthlyv和mobilet来获得这两件事的价值(移动顾问和月度访问者)