Java: Java文本字段 onkeydown 的 if-else 条件下的脚本方法未被调用
Java: Javascript method in if-else condition on textfield onkeydown not being called
我正在 html 中的 wicket 组件上调用 javascript 方法 mehtod1()
。我必须根据下拉列表中的选定选项调用该方法。
<select wicket:id="user.type" id="user.type">
<option>dummy</option>
</select>
<input type="text" wicket:id="identity" id="identity"
onkeydown="if(document.getElementById('user.type').options[document.getElementById('user.type').selectedIndex].value=='52')return method1('abc');
else return method1('xyz');"/>
但是 if-else 条件不起作用。如果我删除此条件并仅调用 method1()
那么它就可以正常工作。我认为 getElementById
不起作用,这就是跳过条件并且根本不调用 method1()
的原因。
Java:
identityField = (TextField<String>) new TextField<String>("identity", new Model()).add(new ErrorIndicator());
identityField.setOutputMarkupId(true);
userTypeDropDown = (LocalizableLookupDropDownChoice<String>) new LocalizableLookupDropDownChoice<String>("user.type", String.class, "abc", this,
false, true, mobBasePage.getLocale()).setNullValid(true).add(new ErrorIndicator());
userTypeDropDown.setDefaultModel(new Model<String>());
userTypeDropDown.setRequired(true);
userTypeDropDown.setOutputMarkupId(true);
userTypeDropDown.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
...
}
}
我无法删除 setOutputMarkupId(true)
,因为我还需要在组件上调用 ajax。
userTypeDropDown.setMarkupId("user.type");
添加这个..这将解决您的问题。
我正在 html 中的 wicket 组件上调用 javascript 方法 mehtod1()
。我必须根据下拉列表中的选定选项调用该方法。
<select wicket:id="user.type" id="user.type">
<option>dummy</option>
</select>
<input type="text" wicket:id="identity" id="identity"
onkeydown="if(document.getElementById('user.type').options[document.getElementById('user.type').selectedIndex].value=='52')return method1('abc');
else return method1('xyz');"/>
但是 if-else 条件不起作用。如果我删除此条件并仅调用 method1()
那么它就可以正常工作。我认为 getElementById
不起作用,这就是跳过条件并且根本不调用 method1()
的原因。
Java:
identityField = (TextField<String>) new TextField<String>("identity", new Model()).add(new ErrorIndicator());
identityField.setOutputMarkupId(true);
userTypeDropDown = (LocalizableLookupDropDownChoice<String>) new LocalizableLookupDropDownChoice<String>("user.type", String.class, "abc", this,
false, true, mobBasePage.getLocale()).setNullValid(true).add(new ErrorIndicator());
userTypeDropDown.setDefaultModel(new Model<String>());
userTypeDropDown.setRequired(true);
userTypeDropDown.setOutputMarkupId(true);
userTypeDropDown.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
...
}
}
我无法删除 setOutputMarkupId(true)
,因为我还需要在组件上调用 ajax。
userTypeDropDown.setMarkupId("user.type");
添加这个..这将解决您的问题。