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

1か月以内にこれを解決しなかった場合。ドキュメントのロード後に選択したアイテムが表示されない理由は、selectpicker イニシャライザを 1 回呼び出す必要があるのに 2 回呼び出しているためだと思います。

通常どおり select を設定し、 document.ready内でselectpickerを呼び出すだけです。例えば:

私のJSPでは、配列を使用してサーブレットを通過する曜日を選択するための複数選択があり、それらのいくつかを選択したい:

<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 に答える