SumoSelect不选择数组

时间:2018-01-19 20:18:50

标签: javascript jquery html arrays sumoselect.js

我正在使用SumoSelect。我有一系列要选择的值。

var physicianIds = [10,11,12,13];

这是html:

<select class="form-control SumoUnder" id="PhysicianID" multiple="multiple" name="CycleDetails.ProcedureRoomPhysicianIds">
    <option value="1">MD01</option>
    <option value="10">MD10</option>
    <option value="11">MD11</option>
    <option value="12">MD12</option>
    <option value="13">MD13</option>
    <option value="14">MD14</option>
    <option value="15">MD15</option>
    <option value="16">MD16</option>
    <option value="17">MD17</option>
    <option value="18">MD18</option>
    <option disabled="disabled" value="19">MD19</option>
    <option value="2">MD02</option>
    <option disabled="disabled" value="20">MD20</option>
    <option value="3">MD03</option>
    <option value="4">MD04</option>
    <option value="5">MD05</option>
    <option value="6">MD06</option>
    <option value="7">MD07</option>
    <option value="8">MD08</option>
    <option value="9">MD09</option>
</select>

我的js:

$(document).ready(function () {
    $('#PhysicianID').SumoSelect();

    for (var i = 0; i < window.physicianIds.length; i++) {
        var physicianId = window.physicianIds[i];
        $('#PhysicianID')[0].sumo.selectItem(physicianId);
    }
});

根据SumoSelect documentation,我希望选择以下内容:

MD10,MD11,MD12和MD13

但是,只选择了MD02和MD03 - 我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

根据文档,您必须使用string按值选择项目。 integer按索引选择项目。

&#13;
&#13;
$('#PhysicianID').SumoSelect();

var physicianIds = [10, 11, 12, 13];
for (var j = 0; j < physicianIds.length; j++) {
  var physicianId = physicianIds[j].toString();  
  $('#PhysicianID')[0].sumo.selectItem(physicianId);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.sumoselect/3.0.2/jquery.sumoselect.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.sumoselect/3.0.2/sumoselect.min.css" rel="stylesheet"/>

<select class="form-control SumoUnder" id="PhysicianID" multiple="multiple" name="CycleDetails.ProcedureRoomPhysicianIds">
  <option value="1">MD01</option>
  <option value="10">MD10</option>
  <option value="11">MD11</option>
  <option value="12">MD12</option>
  <option value="13">MD13</option>
  <option value="14">MD14</option>
  <option value="15">MD15</option>
  <option value="16">MD16</option>
  <option value="17">MD17</option>
  <option value="18">MD18</option>
  <option disabled="disabled" value="19">MD19</option>
  <option value="2">MD02</option>
  <option disabled="disabled" value="20">MD20</option>
  <option value="3">MD03</option>
  <option value="4">MD04</option>
  <option value="5">MD05</option>
  <option value="6">MD06</option>
  <option value="7">MD07</option>
  <option value="8">MD08</option>
  <option value="9">MD09</option>
</select>
&#13;
&#13;
&#13;