我正在使用 javascript 从客户端获取值,使用未绑定(bind)到 wicket 组件的按钮(即,它没有 wicket-id)。现在,我需要将该值设置为文本字段 (textarea) - 这是一个 wicket 组件,但无法做到这一点。如果我在一个简单的文本区域(不是一个 wicket 组件)中设置值,那么它就可以正常工作。但我需要在 wicket 组件中使用该值,以便我可以在提交表单时获取它的值。
Javascript 不适用于 wicket 组件。
Javascript 方法:
var text2 = document.getElementById("e2");
var text1 = document.getElementById("e1");
text1.value = "hello";
text2.value = "hello";
html:
<wicket:panel>
<form wicket:id="form" id="form" onsubmit="submit(event,this);">
<div wicket:id="myPanel">
<input type="button" value="Verify" name="B3" onclick=GetTemplate() />
<p><textarea name="S2" id="e2"></textarea></p>
<p><textarea wicket:id="e1" name="S1" id="e1" ></textarea></p>
<input type="submit" class="submitForm" wicket:id="submit" wicket:message="value:Submit" name="submit" />
</div>
</form>
</wicket:panel>
Java:
public class MyPanel extends Panel {
public MyPanel(...){
...
Form<?> form = new Form("form", new Model());
container = new WebMarkupContainer("myPanel");
textArea = new TextArea<String>("e1", new Model());
textArea.setEnabled(true).setOutputMarkupId(true).
textArea.setOutputMarkupPlaceholderTag(true);
textArea.setEscapeModelStrings(false);
textArea.add(new ErrorIndicator());
submitButton = (Button) new Button("submit") {
private static final long serialVersionUID = 1L;
@Override
public void onSubmit() {
message = textArea.getModelObject();
}
}.setEnabled(true);
container.add(textArea);
container.add(submitButton);
form.add(container);
add(form);
}
在这里,我可以在 textarea id=e2 中设置值,但不能在 id=e1 中设置值。
最佳答案
问题出在这一行:
textArea.setEnabled(true).setOutputMarkupId(true)
这将为 TEXTAREA(用于 Wicket Ajax)生成自定义 ID。因此,id
已更改,您的 javascript 将不再工作,因为它使用被替换的“e1”id。
您可以添加自定义数据属性并通过该属性获取组件。
例如:
<p><textarea wicket:id="e1" name="S1" data-id="e1" ></textarea></p>
然后(例如,使用 jquery,像这样)
$("TEXTAREA[data-id='e1']").val("hello");
或者,如果您不需要 ajax,则跳过调用 setOutputMarkupId
关于javascript - Java:如何将值从 javascript 设置到 wicket 组件文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48763629/