簡體   English   中英

在多選下拉菜單中選擇默認值

[英]Selecting default values in multi-select dropdown

多選問題:我需要在多選下拉菜單中選擇默認選項。 在這里,我正在使用Struts2,默認選項來自服務器。

支撐代碼:

<select multiple >
    <s:iterator var="user" value="%{USERS}"> 
        <option  <s:if test="%{#approvalStep.isUserSelected(#user.userId) == true}"> selected="selected" </s:if> value="<s:property value="#user.userId"/>" > <s:property value="#user.userName"/></option>
    </s:iterator>
</select>

Struts代碼的結果:

<div id="selectAndRemoveUser">
    <select multiple="">
        <option selected="selected" value="2000000002007"> aftabalamm7861111111</option>
        <option selected="selected" value="2000000003015"> aftabalam.m+12</option>
        <option selected="selected" value="2000000003011"> Aftab</option>
        <option selected="selected" value="2000000026353"> aftabalam.m+approver1</option>
    </select>
</div>

問題:當我嘗試使用Jquery獲取選定的選項時,它僅返回第一個選項。 而且,如果我手動編輯select> option元素(我的意思是使用瀏覽器檢查元素重寫selected = selected),則可以正常工作。

jQuery代碼:

console.log($("#selectAndRemoveUser").find('select :selected').length); // the result is 1;

if($("#selectAndRemoveUser").find('select :selected').length > 1 ) {
    alert('false');
} else {
    alert('true');
}
For multiselect dropdown, the name of the select field should be defined as array i.e add 'name[]' at the end of the name attribute.

**HTML** : 
<div id="selectAndRemoveUser">
    <select multiple="" name = "user[]">
        <option value="2000000002007"> aftabalamm7861111111</option>
        <option value="2000000003015"> aftabalam.m+12</option>
        <option value="2000000003011"> Aftab</option>
        <option value="2000000026353"> aftabalam.m+approver1</option>
    </select>
</div>

**Jquery** :

$("select")
  .change(function() {
    var str = "";
    $("select option:selected").each(function() {
      str += $(this).text() + " ";
    });
    console.log(str);
  })
  .trigger("change");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM