首页 > 解决方案 > Java:未调用文本字段onkeydown上if-else条件下的Javascript方法

问题描述

mehtod1()在 html 中的检票口组件上调用 javascript 方法。我必须根据下拉列表中的选定选项调用该方法。

<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()根本没有被调用的原因。

爪哇:

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。

标签: javascriptjavawicket

解决方案


userTypeDropDown.setMarkupId("user.type");

添加这个..这将解决您的问题。


推荐阅读