h:selectmanyListbox 使 "choose" 选项在至少选择了一个选项时无法选择

h:selectmanyListbox make "choose" option unselectable when at least one option is selected

是否可以在 <h:selectManyListbox> 中有一个默认选项,如“--choose--”,当没有选择任何选项时可以选择它。 When the some value is chosen, then it must be unselectable.

<h:selectManyListbox value="#{bean.value}"
        class="form-control">
        <f:selectItems value="#{bean.dropdownValues}" var="value" itemLabel="#{value}" itemValue="#{value}"/>            
</h:selectManyListbox>

只需将其添加为另一个 <f:selectItem> 并请求 JavaScript 的帮助以在更改事件期间选择任何值时将其禁用。

<h:selectManyListbox ... onchange="options[0].disabled=!!value">
    <f:selectItem itemLabel="--choose--" itemValue="#{null}" />
    <f:selectItems ... />
</h:selectManyListbox>

options[0]指的是选择元素的第一个选项。 !!value 基本上将选中的项目值转换为 boolean(当它不是 empty/null 时将是 true),适用于 disabled 属性。

另请参阅:

  • Best way to add a "nothing selected" option to a selectOneMenu in JSF