找到你要的答案

Q:Dynamic dropdown list in the jsp

Q:在JSP动态下拉列表

I have a dropdown list which is dynamic so how can I iterate and insert the list values in the dropdown list?

var questionids = {"Name", "Age", "Smoker", "Drinker", "Visit Denist"};

<select name="questionid" id="questionids">
<c:forEach var="questionids" items="${questionids}">
<option value="">${questionids}</option>
</c:forEach>
</select>

I tried as mentioned above. it is not working. Also I need the value of each option should auto increment like, For first option value is 1 and second option value is 2. e.t.c

Can any one please suggest the solution please

我有一个下拉列表是动态的所以我如何迭代和下拉列表中插入列表的值?

var questionids = {"Name", "Age", "Smoker", "Drinker", "Visit Denist"};

<select name="questionid" id="questionids">
<c:forEach var="questionids" items="${questionids}">
<option value="">${questionids}</option>
</c:forEach>
</select>

我试过如上所述。它不工作。我还需要每个选项的值应该自动递增,第一个选项值是1,第二个选项值是2。等等

任何人请建议的解决方案请

answer1: 回答1:

This is right approach but you have to give another name to var in <c:forEach> like below:

var questionids = {"Name", "Age", "Smoker", "Drinker", "Visit Denist"};

<select name="questionids" id="questionids">
    <c:forEach var="questionid" items="${questionids}">
        <option value="${questionid}">${questionid}</option>
    </c:forEach>
</select>

这是正确的做法,但你必须给另一名VaR在& lt;C:foreach >;像下面:

var questionids = {"Name", "Age", "Smoker", "Drinker", "Visit Denist"};

<select name="questionids" id="questionids">
    <c:forEach var="questionid" items="${questionids}">
        <option value="${questionid}">${questionid}</option>
    </c:forEach>
</select>
answer2: 回答2:
<% java.util.HashMap map = new java.util.HashMap(); 
map.put("0", "Name"); 
map.put("1", "Age"); 
map.put("2", "Smoker"); 
map.put("3", "Drinker"); 
map.put("4", "Visit Denist"); 
pageContext.setAttribute("map", map); 
%>




<select name="questionid" id="questionids" >
    <c:forEach items="${map}">
        <option value="${items.key}">${items.value}</option>
    </c:forEach>
</select>
<% java.util.HashMap map = new java.util.HashMap(); 
map.put("0", "Name"); 
map.put("1", "Age"); 
map.put("2", "Smoker"); 
map.put("3", "Drinker"); 
map.put("4", "Visit Denist"); 
pageContext.setAttribute("map", map); 
%>




<select name="questionid" id="questionids" >
    <c:forEach items="${map}">
        <option value="${items.key}">${items.value}</option>
    </c:forEach>
</select>
answer3: 回答3:
<select name="questionid" id="questionids">
</select>

You can build select using javascript dynamically. You may want to use arr value as option value. If you want to use increment you can create a arr and loop them.

    var questionids = {"Name", "Age", "Smoker", "Drinker", "Visit Denist"};
//  var valuesArr = [1, 2, 3, 4, 5];       
    $.each(questionids , function(i, e) {
        $('#questionids').append("<option value='"+e+"'>"+e+"</option>");
//      $('#questionids').append("<option value='"+valuesArr[i]+"'>"+e+"</option>");
    });
<select name="questionid" id="questionids">
</select>

你可以选择使用JavaScript动态。你可能想使用ARR值作为期权的价值。如果你想使用增量可以创建ARR和循环。

    var questionids = {"Name", "Age", "Smoker", "Drinker", "Visit Denist"};
//  var valuesArr = [1, 2, 3, 4, 5];       
    $.each(questionids , function(i, e) {
        $('#questionids').append("<option value='"+e+"'>"+e+"</option>");
//      $('#questionids').append("<option value='"+valuesArr[i]+"'>"+e+"</option>");
    });
javascript  java  jsp  servlets  jstl