EL表达式两个foreach嵌套循环

集合List中加List泛型的泛型

List<RiskEvaSuitQuestion> riskEvaSuitQuestionList = riskEvaSuitQuestionService.getQueList(1000036L);
model.addAttribute("riskEvaSuitQuestionList",riskEvaSuitQuestionList);

List<RiskEvaSelectItem> riskEvaSelectItemList;
<span>List<List<RiskEvaSelectItem>></span> riskEvaSelectItemLists = new ArrayList<>(); //双重泛型的构建
for (int i = 0; i < riskEvaSuitQuestionList.size(); i++) {
    riskEvaSelectItemList = riskEvaSelectItemService.getByQuestionId(riskEvaSuitQuestionList.get(i).getQuestionId());
    riskEvaSelectItemLists.add(riskEvaSelectItemList);
}
 model.addAttribute("riskEvaSelectItemLists", riskEvaSelectItemLists);

两个foreach可以直接像if一样嵌套用:


下面是一开始以为foreach不能嵌套出现的方法(看起来就头疼):

<c:forEach items="${riskEvaSuitQuestionList}" var="item" varStatus="i">   //第一个foreach
   <p style="font-size: 15px;">${i.count}.${item.content}</p><br/>
   <p style="margin-right: 50px;">
   <c:forEach begin="1" end="${riskEvaSelectItemLists.get(i.count-1).size()}"   varStatus="j" step="1">   //第二个foreach
     <input style="margin-left: 50px;margin-right: 40px;" name="itemContent${i.count-1}" type="radio" value="${riskEvaSelectItemLists.get(i.count-1).get(j.count-1).itemScore}">
     ${riskEvaSelectItemLists.get(i.count-1).get(j.count-1).itemContent}
   </c:forEach>
   </p><br/>
</c:forEach>

注意:javascript中默认加分默认为字符串相加,必须转换为parseInt()

           往input标签中写值,直接定位该标签,然后.value = 值

           javascript中也可以应用域对象取值

<script>
    function calculate() {
        // 题目的总数
        var size = ${riskEvaSuitQuestionList.size()};
        var sum = 0;
        for(var i = 0;i < size;i++){
            var chkObjs = document.getElementsByName("itemContent"+i); // 巧用循环获取radio名字
            for(var j=0;j < chkObjs.length;j++) {
                if (chkObjs[j].checked) {     // radio选中判断.checked
                    sum = parseInt(sum) + parseInt(chkObjs[j].value);
                    break;
                }
            }
        }
        document.getElementById("evaScore").value = sum;

    }
</script>


你可能感兴趣的:(两个foreach嵌套循环)