猿问

多选并选择第一个元素

我已经为 id 多重选择创建了多重选择。我想选择第一个项目作为 selected 。


    <select id="multiselect" multiple="multiple" name="project_list" class="jqmsLoaded ms-list-1" style="display: none;">

<option value="Blue QA">Blue QA</option>

<option value="Easybridge">Easybridge</option>

<option value="K12RS">K12RS</option><option value="LEM">LEM</option>


</select>


 $('#multiselect').multiselect({

        search:true,

        selectAll: true,

        //select : [0], //

        texts: {

          search: 'Search Projects',

           placeholder: 'Select Projects',

      },

      });

我想选择加载时选择的蓝色 QA。选项是动态加载的。


尝试添加为$('#multiselect select option:first-child').prop("selected", true); 但没有运气


慕姐8265434
浏览 108回答 4
4回答

神不在的星期二

需要调用多选时,可以通过再次调用多选的选项来选择多选的选项。$('#multiselect').multiselect({&nbsp; &nbsp; &nbsp; &nbsp; search:true,&nbsp; &nbsp; &nbsp; &nbsp; selectAll: true,&nbsp; &nbsp; &nbsp; &nbsp; texts: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; search: 'Search Projects',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;placeholder: 'Select Projects',&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; var select_1 =&nbsp; $('#multiselect option:eq(0)').val();&nbsp; &nbsp; &nbsp; //selecting element of multiiselect&nbsp; &nbsp; &nbsp; $('#multiselect').multiselect('select', select_1);<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/js/bootstrap-multiselect.min.js" integrity="sha256-qoj3D1oB1r2TAdqKTYuWObh01rIVC1Gmw9vWp1+q5xw=" crossorigin="anonymous"></script><select id="multiselect" multiple="multiple multiselect" name="project_list" class="jqmsLoaded ms-list-1" style="display: none;"><option&nbsp; value="Blue QA">Blue QA</option><option value="Easybridge">Easybridge</option><option value="K12RS">K12RS</option><option value="LEM">LEM</option></select>

千巷猫影

$(&nbsp;document&nbsp;).ready(function()&nbsp;{ &nbsp;&nbsp;&nbsp;$("#multiselect").prop("selectedIndex",&nbsp;0); });这应该可以工作,因为这设置了选定的索引。在prop()函数中,传递第一个参数来选择索引,第二个参数设置为0now,用于指定<option>要选择的索引。因此您可以在其中传递任何现有的索引号。

开心每一天1111

尝试下面的代码$(document).ready(function() {&nbsp;$('#multiselect select option:first-child').prop("selected", true);&nbsp;$('#multiselect').multiselect('refresh');});

鸿蒙传说

只需在加载时添加此脚本,如下所示:-$(function(){$('#multiselect').multiselect('select', $("#multiselect option:first").val()); //second approach $("#multiselect").val($("#multiselect option:first").val());$('#multiselect').multiselect('rebuild');    //important to rebuild if any changes done using script})
随时随地看视频慕课网APP

相关分类

Html5
我要回答