在div中输出我的javascript变量

时间:2013-11-20 07:30:04

标签: javascript html

我想将javascript dayz变量输出到结果div中,但它不输出任何东西,当我在计算之后尝试alert(dayz)时,但是我希望它输出dayz变量html。

..帮助

<html>
    <head>
    <title>My first JavaScript page</title>
    <script type="text/javascript">

    var calculation = function(){

    //days
    var hours = document.getElementById("hours_id").value;
    var d = 10000/hours;
    var da = 1000%hours;
    var dayz = d.toFixed(0);
    dayz = parseInt(dayz, 10);
    dayz = dayz + da;


    //years
    var years = days/365;
    years = years.toFixed(2);

    //weeks
    var weeks = days/7;
    weeks = weeks.toFixed(2);


    };

document.getElementById('results').innerHTML=dayz;

    </script>

</head>
    <body>
        <form name="input">
        <input type="text" id="hours_id" name="hours_id">
        <input type="button" id="submit" value="calculate" onClick="calculation()">  
    </form>



<div id="results"> </div>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

试试这个

    function calculation(){

    //days
    var hours = document.getElementById("hours_id").value;
    var d = 10000/hours;
    var da = 1000%hours;
    var dayz = d.toFixed(0);
    dayz = parseInt(dayz, 10);
    dayz = dayz + da;
    //years
    var years = dayz/365;
    years = years.toFixed(2);
    //weeks
    var weeks = dayz/7;
    weeks = weeks.toFixed(2);
   document.getElementById('results').innerHTML=dayz;

    };

DEMO

答案 1 :(得分:0)

将以下语句放在函数定义中:

document.getElementById('results').innerHTML = dayz;
像这样:

var calculation = function () {
    //days
    var hours = document.getElementById("hours_id").value;
    var d = 10000 / hours;
    var da = 1000 % hours;
    var dayz = d.toFixed(0);
    dayz = parseInt(dayz, 10);
    dayz = dayz + da;
    //years
    var years = dayz / 365;
    years = years.toFixed(2);
    //weeks
    var weeks = dayz / 7;
    weeks = weeks.toFixed(2);
    document.getElementById('results').innerHTML = dayz;
};