Search code examples
jqueryspring-form

jquery doesn't work form:select


I have my form:select like this:

<form:select class="select" path="pmiProfile.proProfileNamePk" name="pmiProfile" value="pmiProfile.proProfileNamePk">
    <option value="ROLE_ADMIN" ><spring:message code="pmi.user.add.admin"></spring:message></option>
    <option value="ROLE_USER" ><spring:message code="pmi.user.add.user"></spring:message></option>
</form:select>

The jquery, I even used the most simple way:

$("select").prop('selectedIndex',1);

What I got was: The chosen index is well set, but the value showed in the combobox is not changed. Why?

Generated HTML, you mean this?

<div class="form-margin-ten">
    <div class="select2-container select" id="s2id_pmiProfile.proProfileNamePk">
        <a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabindex="-1">
        <span class="select2-chosen">User</span><abbr class="select2-search-choice-close"></abbr>
        <span class="select2-arrow"><b></b></span></a>
        <input class="select2-focusser select2-offscreen" type="text" id="s2id_autogen1">
    </div>
    <select id="pmiProfile.proProfileNamePk" name="pmiProfile.proProfileNamePk" class="select select2-offscreen" tabindex="-1">
        <option value="ROLE_ADMIN">Admin</option>
        <option value="ROLE_USER">User</option>
    </select>
</div>

Solution

  • Try something like: $("select option:eq(1)").prop("selected", true)