微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Wicket:如何在textarea的onkeyup上更改标签的文本?

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

莱奥尼德几乎就在那里.结果代码是:

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 lightweight markup language沙箱.

感谢帮助!

解决方法:

如果要在AJAX事件后更新组件,则必须执行以下两项操作:

>可更新组件必须设置标志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我不了解你代码的很多部分.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐