为jsp中的每个循环获取内部隐藏字段的值

get value of hidden field inside for each loop in jsp

本文关键字:隐藏 内部 字段 获取 jsp 循环      更新时间:2023-09-26

希望有人能给我指明正确的方向。

我有一个jsp,它显示一个带有多选答案的单一问题,就像一个调查。一次只显示一个问题。这就是代码。

<c:forEach items="${questions}" var="question" varStatus="loop1">
    <div id="offer-quiz-${loop1.index}" class="offer-quiz-main">Give us your valuable opinion!
    <%
       count++;
       pageContext.setAttribute("count", count);
    %>
  <div id="offer-quiz-hd" div class="offer-quiz-hdr">Question ${count}: </div>
    <div class="offer-quiz-ques">${question.values}</div>
        <div class="offer-quiz-ans">
          <c:forEach items="${question.answers}" var="answerswer" varStatus="loop2">
           <span><input name="qa${loop1.index}" id="radio_${loop1.index}${loop2.index}" type="radio" value="${answer.answers}">
            <label for="radio_${loop1.index} ${loop2.index}">${answer.answers}</label>
           </span>
            <div><input id="nextQId-${loop1.index}${loop2.index}" type="hidden" value="${answer.nextqid}"></div>
          </c:forEach>
          <div class="offer-quiz-btns">
            <a href="#" class="quiz-btn-g">Go Back</a>
            <a href="#" class="quiz-btn">Next</a>
          </div>
       </div>
   </div>
</c:forEach>

我从数据库中传递问题和答案,并将其作为列表传递给jsp。我把问题循环一遍,然后用第二个循环得到答案。我在第二个循环中有一个隐藏的div:

因此,对于每个答案,这个字段的值都是不同的。我想在单击按钮时用javascript获取该字段的值。

有人能帮帮我吗?

非常感谢

由于隐藏按钮和单选按钮中有相同的索引,因此可以使用它来使用简单的javascript获取隐藏按钮的值。

<script>
    function getHiddenValue(answer){
     var index = answer.id.indexOf("_");
     var id = answer.id.substring(index+1);
     alert(document.getElementById("nextQId-"+id).value);  
    }
</script>

单选按钮

<input name="qa${loop1.index}" id="radio_${loop1.index}${loop2.index}" type="radio" value="${answer.answers}" onclick="getHiddenValue(this);">

下面是一个JFiddle示例。