I am trying to collect checkbox values from a popup window and pass their values to the parent window using this code:

 <c:forEach var="row" items="${result2.rows}">
        <input type="checkbox" name="chkBox" id="cb1" value="${row.first_name} ${row.last_name}" />
        <c:out value="${row.first_name} ${row.last_name}"/><br>
    </c:forEach>
<input type="button" value="select"  onclick="javascript:return returnValues()" />

this jstl code works well and display the check boxes and the names even if the result contains many rows.

after choosing the check boxes, I pass their values to the parent window through this java script code:

  function returnValues() {
            if(document.getElementById("cb1").checked){
                var v1 =[];
                v1 =document.getElementById("cb1").value
            }            
            window.opener.setValue(v1);
            window.close();
            return false;
        }

this java script code works perfectly when the returned result contains only one row otherwise it fails.

I tried to rewrite my question and hope I explained it better than before. any help will be appreciated, thanks in advance.

有帮助吗?

解决方案

The JS code doesn't make much sense. getElementById() returns 0 or 1 DOM element: the element which has the ID passed as argument, and which is supposed to be unique in the whole document.

You have several checkboxes, and assigned the same ID to each of them, which makes your HTML code invalid. Since you want to get the values of all the checked checkboxes, you need to iterate on them. To find them, you can use their name, since they all share the same one. At each iteration, you'll determine if the current checkbox is checked, get its value attribute, and add it to an array. And you can then pass this array to your parent window.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top