【发布时间】:2012-09-17 12:04:15
【问题描述】:
在我的检票口应用程序中,我有 3 个复选框:
add(new CheckBox("1").setOutputMarkupId(true));
add(new CheckBox("2").setOutputMarkupId(true));
add(new CheckBox("3").setOutputMarkupId(true));
表单还包含取消选择复选框的行为
add(new AjaxEventBehavior("onclick") {
private static final long serialVersionUID = 1L;
@Override
protected void onEvent(AjaxRequestTarget target) {
List<Component> components = new ArrayList<Component>();
if (target.getLastFocusedElementId() != null) {
if (target.getLastFocusedElementId().equals("1")) {
components.add(get("2"));
components.add(get("3"));
} else if (target.getLastFocusedElementId().equals("2")) {
components.add(get("1"));
} else if (target.getLastFocusedElementId().equals("3")) {
components.add(get("1"));
}
for (Component component : components) {
component.setDefaultModelObject(null);
target.add(component);
}
}
}
});
这在 mozilla 浏览器上运行良好,但在 chrome 中这不起作用。我怎样才能改进在 chrome 上工作呢?
更新
问题出在:
target.getLastFocusedElementId()
在 mozilla 中返回我想要的,但在 chrome 中它总是返回 null 但我不知道 wh
更新 2
谷歌浏览器在焦点元素中有错误:
所以我需要以其他方式这样做
【问题讨论】:
-
根据
AjaxRequestTarget的source code,看起来getLastFocusedElementId()依赖于请求中Wicket-FocusedElementId标头的值。你检查标题了吗? -
ID 必须以字母开头。
-
在不太了解您想要实现的目标的情况下,我发现您发布的代码中有一些奇怪的地方。仅仅为了取消选中某些复选框而触发 Ajax 请求可能有点矫枉过正,为什么不直接在客户端对这种行为进行编码呢?另外,您真的需要检查哪个复选框最后聚焦吗?行为附加到哪个组件?如果您对您要实现的目标提供更多背景信息,那么对您的帮助会更容易。
-
好的上下文:我有 2 个复选框处于关闭状态。它们不能一起检查,因此当客户端检查时,应取消检查,反之亦然。
-
我会完全在客户端实现这个,在提交时为那些没有启用 JS 的用户进行服务器端验证。如果你真的想在服务器端做,为什么不为每个复选框附加不同的行为呢?
标签: java google-chrome wicket