简体   繁体   English

引导选择选择器多选

[英]bootstrap select picker multiple select

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.我正在将字符串从 servlet 传递到脚本。

I have a selectbox in my html with an id, Project1, and class, selectBox.我的 html 中有一个选择框,其 ID 为 Project1,类为 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). projectindex 是从 servlet 传递的变量(使用 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!谢谢你的帮助!

In case you haven't solved this in a month.如果你在一个月内还没有解决这个问题。 I think the reason why it doesn't show the selected items after the document loads is because you are calling selectpicker initializer two times when you should call it once.我认为它在文档加载后不显示所选项目的原因是因为您在应该调用一次时调用了两次 selectpicker 初始值设定项。

You just have to populate your select as you would normally and just call the selectpicker inside the document.ready .您只需要像往常一样填充您的选择,只需在document.ready 中调用selectpicker 即可 For example:例如:

In my JSP, I have a multiple select to choose days of the week which I pass through the servlet using an array, and I want some of them to be selected:在我的 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>

Where weekDays is an array containing the names of the days of the week and days is a List with some of the days.其中weekDays是一个包含星期几名称的数组, days是一个包含某些日子的 List。

And in Javascript I just have this:在 Javascript 中,我只有这个:

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

And it shows alright.它显示正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM