简体   繁体   English

jQuery / Grails将多个选项/值标记为在多个选择框中选择

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

I have this multiple select: 我有多个选择:

        <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>

Previously I was choosing just a single select option based off of a single value: 以前,我只是基于单个值来选择单个选择选项:

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

where hashNames[item][13] = a single value such as I and the select was just a single select box. 其中hashNames [item] [13] =单个值(例如I),而select只是单个选择框。

Now I need it as a multiple select but I still need to accomplish marking them as selected by doing: 现在,我需要将其作为多选,但我仍然需要通过执行以下操作将它们标记为已选中:

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

but now where hashNames[item][13] can look like just a single value such as I or it could be multiple values such as I,S. 但是现在hashNames [item] [13]看起来像是单个值(例如I),也可能是多个值(例如I,S)。

Any help on how to accomplish this? 对如何实现这一目标有帮助吗?

I ended up figuring out a solution shown below: 我最终找到了如下所示的解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM