简体   繁体   中英

Java- determine which radio button selected

I am dynamically filling a table which contains a radio button in each row. How can I go about determining in which row the radio button has been selected using Java ?

<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. 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. 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. After that iterate the ArrayList in your JSP to display the properties along with radio button. Rest of all is explained in given link. Note: Your bean class must have a property with proper getter/setter for the radio button.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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