简体   繁体   English

Java-确定选择哪个单选按钮

[英]Java- determine which radio button selected

I am dynamically filling a table which contains a radio button in each row. 我正在动态填充一个表格,该表格的每一行都包含一个radio button How can I go about determining in which row the radio button has been selected using Java ? 如何确定使用Java选择radio button

<form action='ReceptionManagerController' method='POST'>
        <table >
            <tr style="text-align:center">
                     <th>Check-In</th>
                     <th>Check-Out</th>
                     <th>Type</th>
                     <th>Assign Room?</th>
            </tr>
            <c:forEach var="item" items="${unassignedBookings}">
                <tr>
                    <td>${item.checkIn}</td>
                    <td>${item.checkOut}</td>
                    <td>${item.size}</td>
                    <td><input type="radio" name="checked"></td>    
                    <td><input type="hidden" name="id${count}"></td>    
                    <td><input type="hidden" name="checkIn${count}" value="${item.checkIn}"></td>   
                    <td><input type="hidden" name="checkOut${count}" value="${item.checkOut}"></td>     
                    <td><input type="hidden" name="type${count}" value="${item.size}"></td> 
                    <c:set var="count" value="${count + 1}" scope="page"/>  
                </tr>                       
            </c:forEach>


        </table>

        <input  type="submit" name="action" value="Assign Booking"/>
    </form>

If I give each radio button the same name, then only one can be selected at a time. 如果我为每个radio button赋予相同的名称,则一次只能选择一个。 However, this stops me from determining uniquely which button has been pressed. 但是,这使我无法唯一确定按下了哪个按钮。

Any suggestions? 有什么建议么?

Thank you for your help. 谢谢您的帮助。

I had a similar problem last year while working on a struts 1.3 based web application. 去年,我在基于Struts 1.3的Web应用程序上工作时遇到类似的问题。 You can refer to the this link. 您可以参考链接。 I hope it will help you. 希望对您有帮助。 If still its not clear to you, let me put some try on it. 如果您仍然不清楚,请让我尝试一下。 You can take an ArrayList of your bean type(Using Generics) having the setter and getter for it and keep the appropriate values for all the bean objects and add all those objects one by one in your ArrayList. 您可以使用具有setter和getter的bean类型(使用泛型)的ArrayList,并为所有bean对象保留适当的值,并将所有这些对象一一添加到ArrayList中。 After that iterate the ArrayList in your JSP to display the properties along with radio button. 之后,迭代JSP中的ArrayList以显示属性以及单选按钮。 Rest of all is explained in given link. 其余所有内容在给定的链接中进行了说明。 Note: Your bean class must have a property with proper getter/setter for the radio button. 注意:您的bean类必须具有一个带有正确的用于单选按钮的getter / setter的属性。

By the form that I can see, Check-Box seems to be appropriate choice rather than Radio Button. 通过我可以看到的形式,复选框而不是单选按钮似乎是合适的选择。 As far as capturing the selection is concerned, you can follow same strategy that you are doing for rest of the form elements, suffix count value to radio-Button or check-box to give them uniqueness. 就捕获选择而言,您可以采用与其余表单元素相同的策略,在单选按钮或复选框后缀计数值以使其具有唯一性。

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

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