0

I have a bootstrap selectpicker and what I am trying to do is get multiple options selected once I load the page. I am passing in a string from a servlet to a script.

I have a selectbox in my html with an id, Project1, and class, selectBox.

In my script:

$(document).ready(function() {
  $('.selectBox').selectpicker();
});

var index2="${projectindex}";
$('#Project1').selectpicker('val',index2);

projectindex is the variable being passed from the servlet (using jsp). I checked it and it passes correctly to something similar to this:

['project1' , 'project2']

These two are values in the select box, but they are not selected once the document loads. Can someone tell me what I did wrong?

Thanks for any help!

4

1 回答 1

0

如果你在一个月内没有解决这个问题。我认为它在文档加载后不显示所选项目的原因是因为你调用了 selectpicker 初始化程序两次,而你应该调用它一次。

您只需像往常一样填充您的选择,然后在document.ready中调用selectpicker。例如:

在我的 JSP 中,我有一个多重选择来选择我使用数组通过 servlet 的星期几,并且我希望选择其中的一些:

<select class="selectpicker" multiple name="dayGroup" title="Select days">
  <c:forEach var="weekDay" items="${weekDays}">
    <option value="${weekDay}" ${fn:contains(days, weekDay) ?'selected' : ''}>${weekDay}</option>
  </c:forEach>
</select>

其中weekDays是一个包含星期几名称的数组,而days是一个包含某些日子的列表。

在Javascript中我只有这个:

$('.selectpicker').selectpicker();

它显示正常。

于 2013-07-23T16:21:19.320 回答