Wicket:如何将DropDownChoice与PropertyModel一起使用



我在使用带有DropDownChoice的PropertyModel时遇到问题。我需要一些东西,每次DropdownChoice更改时,它都会比较值并做一些事情。这就是我正在尝试的:

实例化选项

private String typeSelected = DEFAULT_TYPE_VALUE;
List<String> typeSelectChoices = new ArrayList<String>();
typeSelectChoices.add(DEFAULT_TYPE_VALUE);
typeSelectChoices.add(TextFactory.TYPE_VALUE_1;
typeSelectChoices.add(TextFactory.TYPE_VALUE_2);

实例化DropDownChoice

typeSelect = new DropDownChoice<String>(TYPE_SELECT_ID, 
                    new PropertyModel(this, "typeSelected"), typeSelectChoices);

DropDownChoiceonChange事件

typeSelect.add(new AjaxEventBehavior("onchange") {
        @Override
        protected void onEvent(AjaxRequestTarget target) {
            System.out.println(typeSelected);
        }
    });

它总是打印"选择",无论选择的选项是什么。我也试过:

System.out.println(typeSelect.getModel.getObject());

我不是这里的专家,但看起来你需要引用typeSelected的属性,而不是typeSelected本身。所以应该是这样的:

System.out.println(typeSelected.value);

正如我所说,我不是专家,所以我不确定.value是正确的属性,但你应该能够从这里弄清楚。

祝你好运!


更新的

这适用于Javascript:

System.out.println(typeSelected.options[typeSelected.selectedIndex].value);

您可以在DropDownChoice的方法wantOnSelectionChangedNotifications的javadoc中找到一些其他有用的信息。

最新更新