Wicket:如何更改文本区域 onkeyup 上的标签文本?
如何更改文本区域 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") );
}
}
解决方案
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
最后一个问题是我对添加 renderComponent() 的糟糕直觉 - 由于某种原因,标签保持不变。
顺便说一句,结果将很快作为 JTexy 轻量级标记语言沙箱提供。
感谢您的帮助!
How do I change label's text on textarea's onkeyup? I've tried this but does not work:
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
leonidv was almost there. The resulting code is:
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.
By the way, the result will serve soon as JTexy lightweight markup language sandbox.
Thanks for help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想在 AJAX 事件后更新组件,你必须做两件事:
setOutputMarkupId == true;
你必须将此组件添加到目标 onEvent 方法
PS 我不明白您代码的很多部分。
If you want to update components after AJAX event, you must to do 2 things:
setOutputMarkupId == true;
You must add this components to target onEvent method
PS I don't understand many parts of your code.
而不是 resultDiv.renderComponent();
尝试 resultDiv.modelChanged();
instead of resultDiv.renderComponent();
try resultDiv.modelChanged();