wicket ajaxformcomponentupdatingbehavior通过单击按钮获取TextField的值

时间:2013-07-22 08:24:03

标签: java forms wicket

我有一个表单,并且有一个TextField。我在表单中添加了按钮,与默认提交不同。我想在单击TextField旁边的按钮后使用Ajaxformcomponentupdatingbehavior从TextField获取值。

我的代码如下:

private String string;
...
public ..() {   
Form form = new Form("form") {
            @Override
            protected void onSubmit() { 
         //some code
};

add(form);
TextField textField = new TextField("string", new PropertyModel<String>(this,"string"));
 textField.setOutputMarkupId(true);
form.add(textField);
Button button = new Button("evalButton");   
form.add(button);
button.add(new AjaxFormComponentUpdatingBehavior("onclick") {
            @Override
            protected void onUpdate(AjaxRequestTarget target) {               
                System.out.print(textField.getValue());
             }
});

TextField的值为null,在第二次单击该按钮后,我得到了正确的值。单击一次按钮后如何获取TextField的值?

1 个答案:

答案 0 :(得分:1)

AjaxFormComponentUpdatingBehavior没有做到你想的那么多。该行为实际上应用于TextField,而不是按钮。您的代码正在更新按钮的模型而不是文本。有关示例,请参阅this previous question

之前我已经为地址表单中的邮政编码查找按钮做了这个。我用一个`IndicatingAjaxButton'来推送整个表单,我禁用了默认的表单处理。然后我直接抓取文本输入,将其推送到我的验证器,该验证器标准化格式,然后处理:

final IndicatingAjaxButton lookup = new IndicatingAjaxButton("lookup", form) {
  @Override
  protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
    String code = postcode.getInput();

    code = (new PostcodeValidator()).convertToObject(code,
                    getLocale());

    ... Postcode lookup here


    target.add(ContactDetailsPanel.this);
  }

  @Override
  protected void onError(AjaxRequestTarget target, Form<?> form) {
  }
};
lookup.setDefaultFormProcessing(false);
add(lookup);
相关问题