为什么我无法从javascript中获得输入值

时间:2017-09-24 00:19:09

标签: javascript html

为什么我在htm给出的输入标签中计算后得不到javascript值

`id=""result"` 

虽然我在span id中使用document.write(num);

<?php include ('header.php'); ?>
<!DOCTYPE html>
<html> 
<head>
<meta charset=utf-8 />
<title>Calculator </title>
</head>
<body>
<form>
<table>
    <tr>
        <td>
            <label style="padding-top: 5px" >Enter Your Per KG Price : </label>
        </td>
        <td  style="padding-left:15px;">
            <input type="tex" id="perkg" placeholder="Enter Per KG Price">
        </td>
    </tr>
    <tr>
        <td>
            <label style="padding-top: 10px" >Enter Your Price : </label>
        </td>
        <td  style="padding-left:15px; padding-top: 10px">
            <input id="required" placeholder="Desired Amount">
        </td>
    </tr>
    <tr>
        <td>
            <input type="button" onClick="multiply()" Value="Multiply" />
        </td>
    </tr>
        <td>
            <label style="padding-top: 10px" > Weight Given To Customer In Grams </label>
        </td>
        <td  style="padding-left:15px; padding-top: 10px">
            <input id="result"  value="" placeholder="In Grams">
        </td>
    </tr>
</table>


<input type="button" onClick="divideBy()" Value="Divide" />
<p>The Result is : <br>
<span id = "results"></span>
</p>
</form>





<script type="text/javascript">
function multiply()
{
        var num1 = document.getElementById("perkg").value;
        var num2 = document.getElementById("required").value;
        var num =  (1000/num1) * num2;
        document.getElementById("result").innerHTML = num;
        //document.write(num); its working

}
</script>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

更新输入 值属性使用value属性而不是innerHTML

document.getElementById("result").value = num;