【问题标题】:wicket ajaxformcomponentupdatingbehavior get value of TextField by clicking buttonwicket ajaxformcomponentupdatingbehavior 通过单击按钮获取 TextField 的值
【发布时间】:2013-07-22 08:24:02
【问题描述】:

我有一个表单,还有一个 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的值?

【问题讨论】:

    标签: java forms wicket


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 2018-04-28
      相关资源
      最近更新 更多