我正在 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");
添加这个..这将解决您的问题。
关于javascript - Java:未调用文本字段 onkeydown 上 if-else 条件下的 Javascript 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50188214/