2013-07-22 85 views
1

我有一个表格,有一个文本字段ajaxformcomponentupdatingbehavior获取文本字段的值。我将按钮添加到表单,与默认提交不同。在单击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的值?

回答

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); 
+0

太好了,它的工作原理与我需要的一样,谢谢..;) –