4

如何在 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") );
    }
  }

解决方案

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 轻量级标记语言沙箱。

感谢帮助!

4

2 回答 2

4

如果你想在 AJAX 事件之后更新组件,你必须做两件事:

  1. 可更新组件必须设置标志setOutputMarkupId == true;
  2. 您必须将此组件添加到目标 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我不明白你的代码的很多部分。

于 2010-01-22T14:28:34.983 回答
0

而不是 resultDiv.renderComponent(); 试试 resultDiv.modelChanged();

于 2010-01-22T13:01:59.550 回答