JQuery/Grails 将多个选项/值标记为在多个选择框中处于选中状态

JQuery/Grails marking multiple options/values as selected in multiple select box

本文关键字:选择 状态 于选中 Grails 选项 JQuery 记为      更新时间:2023-09-26

我有这个多选:

        <select type="text" name="specialTypeInd" id="specialTypeInd" multiple>
          <option value="">None</option>
          <option value="I">International</option>
          <option value="M">Minority</option>
          <option value="S">Study Abroad</option>
        </select>

以前,我只根据单个值选择单个选项:

document.getElementById('specialTypeInd').value = hashNames[item][13]

其中 hashNames[item][13] = 单个值,例如 I,而选择只是一个选择框。

现在我需要它作为多项选择,但我仍然需要通过执行以下操作将它们标记为已选择:

document.getElementById('specialTypeInd').value = hashNames[item][13]

但现在 hashNames[item][13] 可能看起来只是一个值,例如 I,也可以是多个值,例如 I,S。

关于如何完成此操作的任何帮助?

我最终找到了如下所示的解决方案:

$.each(hashNames[item][13].split(","), function(i,e){
        $("#specialTypeInd option[value='" + e + "']").prop("selected", true);
});