java—使用单选按钮更改记录

lndjwyie  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(255)

如何使用单选按钮更改和更新记录列表?我有一个学生对象列表(studentlist),它在ui中显示如下:

所以现在我改变汤姆和萨利的成绩,点击“更新”。如何将这些信息传递回servlet?

public class Student{
    private String name;
    private String grade;
    //getters setters
}

我可以使用文本字段实现更新功能,但是单选按钮看起来很棘手。
我用 c:forEach 反复浏览学生名单。

<c:forEach var="student" items="${studentList}">
    <tr>
        <td>Tom</td>
        <td>
            <label for=""><input type="radio" name="tom" checked="checked"/>A</label>
            <label for=""><input type="radio" name="tom"/>B</label>
            <label for=""><input type="radio" name="tom"/>C</label>
            <label for=""><input type="radio" name="tom"/>D</label>
        </td>
    </tr>
    <tr>
        <td>Sally</td>
        <td>
            <label for=""><input type="radio" name="sally" checked="checked"/>A</label>
            <label for=""><input type="radio" name="sally"/>B</label>
            <label for=""><input type="radio" name="sally"/>C</label>
            <label for=""><input type="radio" name="sally"/>D</label>
        </td>
    </tr>
    .
    .
    .
    <tr></tr>

<button type="submit">Update</button>
qhhrdooz

qhhrdooz1#

你得给我个建议 value 到每个单选按钮。例如:

<!-- Using a single example -->
<!-- fixed the label issue in your HTML -->
<input type="radio" id="tom_A" name="tom" value="A" /> <label for="tom_A">A</label>
<input type="radio" id="tom_B" name="tom" value="B" /> <label for="tom_B">B</label>

然后,在servlet中,您将使用 request.getParameter :

String tomCalification = request.getParameter("tom");
System.out.println(tom);
//will print A if selected radio button with A
//will print B if selected radio button with B

其他情况也一样。

相关问题