java Wicket:如何更改 textarea 的 onkeyup 上的标签文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2117243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wicket: How to change label's text on textarea's onkeyup?
提问by Ondra ?i?ka
How do I change label's text on textarea's onkeyup? I've tried this but does not work:
如何更改 textarea 的 onkeyup 上的标签文本?我试过这个,但不起作用:
Form form;
TextArea ta;
MyLabel resultDiv;
/**
* Constructor that is invoked when page is invoked without a session.
*/
public HomePage(final PageParameters parameters) {
this.form = new Form("form");
this.ta = new TextArea("text");
this.resultDiv = new MyLabel("result");
this.ta.add( new AjaxEventBehavior( "onKeyUp" ) {
protected void onEvent( AjaxRequestTarget target ) {
System.out.println( "Ajax!" );
resultDiv.setText("Foobar");
resultDiv.renderComponent();
}
} );
form.add( ta );
form.add( resultDiv );
add( form );
}// const
public class MyLabel extends Label {
private String text = "original";
public String getText() { return text; }
public void setText( String text ) { this.text = text; }
public MyLabel( String id ) {
super( id );
this.setModel( new PropertyModel(this,"text") );
}
}
Solution
解决方案
leonidvwas almost there. The resulting code is:
leonidv 快到了。结果代码是:
Form form;
TextArea ta;
Label resultDiv = new Label( "result", new PropertyModel(this,"labelText") ){
{ setOutputMarkupId( true ); }
};
private String labelText = "original";
/**
* Constructor that is invoked when page is invoked without a session.
*/
public HomePage(final PageParameters parameters) {
this.form = new Form("form");
this.ta = new TextArea("text");
this.ta.add( new AjaxEventBehavior( "onKeyUp" ) {
protected void onEvent( AjaxRequestTarget target ) {
System.out.println( "Ajax!" );
labelText = "Foobar"; // Doesn't even need get/set, which is great.
target.addComponent( resultDiv );
//resultDiv.renderComponent(); // WRONG!!
}
} );
form.add( ta );
form.add( resultDiv );
add( form );
}// const
The last problem was my bad intuition about adding renderComponent()- that, for some reason, kept the label unchanged.
最后一个问题是我对添加的错误直觉renderComponent()——出于某种原因,保持标签不变。
By the way, the result will serve soon as JTexy lightweight markup languagesandbox.
顺便说一下,结果将很快用作JTexy 轻量级标记语言沙箱。
Thanks for help!
感谢帮助!
采纳答案by leonidv
If you want to update components after AJAX event, you must to do 2 things:
如果你想在 AJAX 事件之后更新组件,你必须做两件事:
- Updatable components must have setted flag
setOutputMarkupId == true; You must add this components to target onEvent method
this.resultDiv.setMarkupOutputId(true); protected void onEvent( AjaxRequestTarget target ) { System.out.println( "Ajax!" ); //resultDiv.setModel( ); resultDiv.setText("Foobar"); resultDiv.renderComponent(); target.add(resultDiv); }
- 可更新组件必须设置标志
setOutputMarkupId == true; 您必须将此组件添加到目标 onEvent 方法
this.resultDiv.setMarkupOutputId(true); protected void onEvent( AjaxRequestTarget target ) { System.out.println( "Ajax!" ); //resultDiv.setModel( ); resultDiv.setText("Foobar"); resultDiv.renderComponent(); target.add(resultDiv); }
PS I don't understand many parts of your code.
PS我不明白你的代码的很多部分。
回答by Pawel Szulc
instead of resultDiv.renderComponent(); try resultDiv.modelChanged();
而不是 resultDiv.renderComponent(); 尝试 resultDiv.modelChanged();

