javascript - Java:未调用文本字段 onkeydown 上 if-else 条件下的 Javascript 方法

标签 javascript java wicket

我正在 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/

相关文章:

javascript - 为什么ExtJS要测试浏览器是否支持排序?

javascript - HTML/Javascript : How to add a live video stream from another website

javascript - Internet Explorer 错误 SCRIPT1014 无效字符`

javascript - 为焦点创建事件观察器?

java - 如何每5分钟调用一个函数?

java - echo $JAVA_HOME 什么都不返回

Java:解析文件路径

testing - Wicket 口和AtUnit

java - wicket 1.5 新网址