twitter-bootstrap 引导选择选择器多选

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17306618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 17:26:10  来源:igfitidea点击:

bootstrap select picker multiple select

javascriptjqueryselecttwitter-bootstrap

提问by kchow23

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). I checked it and it passes correctly to something similar to this:

projectindex 是从 servlet 传递的变量(使用 jsp)。我检查了它,它正确地传递给类似的东西:

['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!

谢谢你的帮助!

回答by Mauricio Farah

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 selectpickerinside the document.ready. For example:

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

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 weekDaysis an array containing the names of the days of the week and daysis a List with some of the days.

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

And in Javascript I just have this:

在 Javascript 中,我只有这个:

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

And it shows alright.

它显示正常。