简体   繁体   中英

javascript table row cell selected dropdown value

I have a table with 3 rows, I want to get the selected dropdown value value of each row. JSP code:

<tr>
        <td style="padding:0px;margin:0px;">
        <select name="lineLineType" id="lineLineType<%= i %>" onchange='javascript:doClose("errorval")' >                                 
       <option value=""> </option>
        <c:forEach items="${savedlineTypes}" var="linetype">                                                                 
             <option value="<c:out value="${linetype.linetypeId}" />" <c:if test="${linetype.linetypeId== line.lineType}">selected</c:if> ><c:out value="${linetype.linetypeId}" /> - <c:out value="${linetype.linetypeDesc}" /></option>
                                                    </c:forEach>
                                            </select>
                                          </td>

I am trying below java script but always I am getting first line value for the drop down. how can I fix this? Java script I am using

   var dd = document.getElementById("lineLineType");
   lineType = dd.options[dd.selectedIndex].value; 

you must give different id for each dropdown. give it like lineLineType1, lineLineType2, lineLineType3 and get them by :

var dd1 = document.getElementById("lineLineType1");
lineType1 = dd1.options[dd1.selectedIndex].value; 
var dd2 = document.getElementById("lineLineType2");
lineType2 = dd2.options[dd2.selectedIndex].value; 
var dd3 = document.getElementById("lineLineType3");
lineType3 = dd3.options[dd3.selectedIndex].value; 

this might be easiest, but not rightest solution. rather than giving id for each dropdown, try to set same class name for all dropdown. for example if you give the class name as "lineLineType", you can easily access them like this :

   var dd = document.getElementsByClassName("lineLineType");
   var dd_data = dd[row_you_want_to_get]; // row_you_want_to_get value should be between 0, 1, 2 in your case
   var lineType = dd_data.options[dd_data.selectedIndex].value;

hope this helps.

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