我有一个列表questionForm
,其中包含question_id
,question
,options
,我将选项作为由|
符号分隔的字符串。任何人都可以给我解决方案,将字符串分开并用单选按钮显示选项。
列表包含
1
java is?
object oriented | Procedural | Asembly | Machin Level
2
Static block loaded?
Afer the class | Before the class | never | No such variable in java
3
Which is a reserved word in the Java programming language?
method | native | subclasses | reference
<c:forEach var="opt" items="${questionForm}">
<c:if test="${opt.question_id==1}">
<script>
check('${opt.question_type}','${opt.options}');
</script>
<td style="padding-left:200px;">
</c:if>
</c:forEach>
我的javascript函数是:
function check1(type,options){
alert(type);
var list = options;
alert(list);
if(type == "multiple choice"){
alert(list);
<% String values = "<script>document.writeln(list)</script>";
List<String> opts = Arrays.asList(values.split("|"));
out.println(opts);
}
}
,预期输出为
答案 0 :(得分:1)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
-------------样本测试数据-----------------
ArrayList<Question> questionForm = new ArrayList<Question>();
Question q1 = new Question(1, "multiple choice", "java is?", "object oriented | Procedural | Asembly | Machin Level" , 1);
Question q2 = new Question(2, "multiple choice", "Static block loaded?", "Afer the class | Before the class | never | No such variable in java", 2);
Question q3 = new Question(3, "multiple choice", "Which is a reserved word in the Java programming language?", "method | native | subclasses | reference", 2);
questionForm.add(q1); questionForm.add(q2); questionForm.add(q3);
request.setAttribute( "questionForm", questionForm );
-------------结束/样本测试数据-----------------
public class Question {
private Integer question_id;
private String question_type;
private String question;
private String options;
private int answer;
...
}
<c:set var="radioNamelist" value="${fn:split('A,B,C,D,E', ',')}" scope="request" />
<table style="float: left" >
<c:forEach var="obj" items="${questionForm}" varStatus="status">
<tr>
<th style="text-align: left">
Q. <c:out value="${obj.question_id}" />. <c:out value="${obj.question}" />
</th>
</tr>
<tr>
<c:if test="${obj.question_type=='multiple choice'}">
<c:set var="options" value="${fn:split(obj.options, '|')}" />
<c:forEach var="opt" items="${options}" varStatus="optStatus" >
<tr>
<td style="padding-left:30px;">
<input type='radio' name='opt${status.count}' value='${optStatus.count}' /> <c:out value="${radioNamelist[optStatus.count-1]}" />. <c:out value="${opt}" />
</td>
</tr>
</c:forEach>
</c:if>
</tr>
</c:forEach>
</table>
答案 1 :(得分:0)
试试这个:
String[] ar=values.split("|");
ArrayList<String> al=new ArrayList<String>();
for(int i=0;i<ar.length;i++)
{
al.add(ar[i]);
}
现在通过运行Iterator(),您可以以选项的形式显示