我正在尝试获得4年级的平均水平,并将其通过文本字段。

它确实得到了平均值,唯一不起作用的部分是grade.value = grade;部分

这是我的代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Week #10 JavaScript Assignment: Problem #1</title>
<script type = "text/javascript">
function calcgrade(one,two,three,four){
    sum = parseInt(one.value) + parseInt(two.value) + parseInt(three.value) + parseInt(four.value);
    grade = sum / 4;
    grade.value = grade;
}
</script>
</head>
<body>
<h1>Calculate Grade</h1>
<form>
    <table>
        <tr>
            <th>Assignment #</th>
            <th>Grade</th>
        </tr>
        <tr>
            <td>1</td>
            <td><input type="text" id="one" name="one"></td>
        </tr>
        <tr>
            <td>2</td>
            <td><input type="text" id="two" name="two"></td>
        </tr>
        <tr>
            <td>3</td>
            <td><input type="text" id="three" name="three"></td>
        </tr>
        <tr>
            <td>4</td>
        <td><input type="text" id="four" name="four"></td>
        </tr>
        <tr><td colspan="2"><input type="button" onclick="calcgrade(one, two, three, four)" value="Calculate Grade"></td></tr>
        <tr>
            <td>Grade:</td>
            <td><input type="text" id="grade" name="grade"></td>
        </tr>
    </td>
</form>
</body>
</html>

最佳答案


document.getElementById('grade').value=grade;

关于javascript - 如何使用javaScript正确设置文本字段的值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19919565/

10-13 04:27