希望有人能指出我正确的方向。
我有一个 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="answer" 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例子。
关于javascript - 为jsp中的每个循环获取内部隐藏字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25949071/