如何在评分表中显示名称

时间:2016-06-02 15:49:03

标签: javascript html

有人可以帮我这个代码吗?

我不知道如何在评分表中显示姓名,课程和年份。这只是一个简单的评分系统。

以下是代码:

<table>
 <tr><td>Student Name:</td><td><input type="text" id="s_n" name="studn"></td></tr>
 <tr><td>Course and Year:</td><td><input type="text" id="c_y" name="crsy"></td></tr>
 <tr><td>Quiz 1:</td><td><input type="text" id="txt1" name="text1"></td></tr>
 <tr><td>Quiz 2:</td><td><input type="text" id="txt2" name="text2"></td></tr>
 <tr><td>Quiz 3:</td><td><input type="text" id="txt3" name="text3"></td></tr>
 <tr><td><button onclick="myFunction()">Total</button></td></tr>
</table>

 <script>
    function myFunction() {
        var stud = document.getElementById("s_n").value;
        var crs = document.getElementById("c_y").value;
        var a = document.getElementById("txt1").value;
        var b = document.getElementById("txt2").value;
        var c = document.getElementById("txt3").value;
        //var d = document.getElementById("txt4").value;
        var total = parseInt(a) + parseInt(b) + parseInt(c);
        document.getElementById("scores").innerHTML = total;
    }
</script>
<p id="scores"></p>

3 个答案:

答案 0 :(得分:2)

您可能想要这样做:

<script>
function myFunction() {
    var stud = document.getElementById("s_n").value;
    var crs = document.getElementById("c_y").value;
    var a = document.getElementById("txt1").value;
    var b = document.getElementById("txt2").value;
    var c = document.getElementById("txt3").value;
    //var d = document.getElementById("txt4").value;
    var total = parseInt(a) + parseInt(b) + parseInt(c);

    document.getElementById("scores").innerHTML = "<div><span class='student'>" + stud + "<span class='course'> " + crs + "</span><span class='total'>Total: " + total + " </span>";
}
</script>

这将生成给定的HTML:

<div>
    <span class='student'>Student Name</span>
    <span class='course'>Course</span>
    <span class='total'>Total: value</span>
</div>

答案 1 :(得分:1)

在HTML中,您可以添加:

<p id="info"></p>

您可以将其添加到JavaScript:

var info = "Name: " + stud + ", Course & Year: " + crs;
document.getElementById("info").innerHTML = info;

答案 2 :(得分:1)

您可以像这样连接总数和名称:

document.getElementById("scores").innerHTML ="Name "+stud+ " total " +total;

参见示例https://jsfiddle.net/2dfempL0/1/