[英]Java: How to set a value from javascript to a wicket component textfield
我正在使用 javascript 从客户端使用未绑定到 wicket 组件的按钮获取值(即,它没有 wicket-id)。 现在,我需要将该值设置为文本字段 (textarea) - 这是一个检票口组件,但无法做到这一点。 如果我在一个简单的 textarea(不是 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>
爪哇:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.