【问题标题】:CSS Select for selectpicker via class ID [closed]通过类 ID 为 selectpicker 选择 CSS [关闭]
【发布时间】:2017-08-11 21:18:30
【问题描述】:

我有两个类:具有不同 ID 的 selectpicker:selected_Test_Platformsselected_SIL_relevant 我想将 selected_Test_Platforms 设置为来自数组的预选 -> availableTestPlatforms

它仅适用于选择器 $('.selectpicker') 但这显然会同时选择两者,这不是我的意图。

那么我该如何选择并仅将 id="selected_Test_Platforms" 设置为数组 availableTestPlatforms。

   //works but selects both IDs 
$('.selectpicker').selectpicker('val', availableTestPlatforms);
    
//does not work
$( "#selected_SIL_relevant" ).selectpicker('val', availableTestPlatforms);
$('select[name=selValue]').selectpicker('val', availableTestPlatforms);
<div class="container">
  <nav class="navbar navbar-default" role="navigation">
    <div class="container-fluid">
      <div class="navbar-header">
        <a class="navbar-brand" href="#">Report manipulation options:</a>
      </div>

      <form class="navbar-form navbar-left" role="search">
        <div class="form-group">
          <select name="selValue" id="selected_Test_Platforms" class="selectpicker" multiple data-live-search="true" data-live-search-placeholder="Search" data-actions-box="true">
          </select>
        </div>
        <div class="form-group">
          <select id="selected_SIL_relevant" class="selectpicker" multiple data-live-search="true" data-live-search-placeholder="Search" data-actions-box="true">
              <option value='-All-' selected>-All-</option>
              <option value='Safety Relevant'>Safety Relevant</option>
              <option value='QM'>QM</option>
          </select>
        </div>
        <button type="button" onclick="main()" class="btn btn-default">Submit</button>
      </form>
    </div>
  </nav>
</div>

【问题讨论】:

  • 如果$('.selectpicker').selectpicker( 有效,那么$("#selected_Test_Platforms").selectpicker 也应该有效。我不知道你为什么只尝试了$("#selected_SIL_relevant.. 并表示如果这不是你的要求,它就不起作用。
  • 谢谢,我只是用 $("#selected_Test_Platforms") 取了错误的 ID 名称,它现在可以工作了。

标签: javascript jquery css css-selectors


【解决方案1】:

使用$('#selected_Test_Platforms') 作为id

【讨论】:

  • 好的,谢谢您的提示,我只是拿错了 ID 名称。 $( "#selected_Test_Platforms" ).selectpicker('val', availableTestPlatforms);现在正在工作。
  • 酷,乐于助人
猜你喜欢
  • 2019-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-24
  • 2017-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多