【问题标题】:Make changes to records using radio buttons.使用单选按钮更改记录。
【发布时间】:2014-04-15 06:36:22
【问题描述】:

如何使用单选按钮更改和更新记录列表? 我有一个学生对象列表(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>

【问题讨论】:

    标签: java javascript jquery jsp radio


    【解决方案1】:

    您需要为每个单选按钮提供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
    

    所有其他情况类似。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-17
      • 2021-06-11
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多