简体   繁体   English

使用单选按钮更改记录。

[英]Make changes to records using radio buttons.

How can I change and update a list of records using radio buttons?如何使用单选按钮更改和更新记录列表? I have a list of student objects(studentList) which is displayed in the UI as follows:我有一个学生对象列表(studentList),它显示在 UI 中,如下所示:

在此处输入图像描述

So now I change the grades of Tom and Sally and hit "Update".所以现在我改变汤姆和莎莉的成绩并点击“更新”。 How do I pass these information back to my servlet?如何将这些信息传递回我的 servlet?

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

I was able to implement the update feature using text field but radio buttons seem tricky.我能够使用文本字段实现更新功能,但单选按钮似乎很棘手。

I use c:forEach to iterate through list of students.我使用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>

You need to give a value to each radio button.您需要为每个单选按钮value For example:例如:

<!-- 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>

Then, in your servlet, you will recover its value using request.getParameter :然后,在您的 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

Similar for all other cases.对于所有其他情况类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM