如何更新< input/>通过AJAX请求在挂毯中的字段 [英] How can update an <input/> field in tapestry by AJAX request

查看:57
本文介绍了如何更新< input/>通过AJAX请求在挂毯中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用挂毯5开发的项目.我需要使用AJAX请求更新输入字段(位于表单内).

I have a project developed with tapestry 5. I need to update a input field (which is inside a form) using an AJAX request.

Page.tml是这样的:

Page.tml is something like this:

<form>
    <t:zone t:id="myZone">
        <input type="text" t:type="TextField" t:value="product.code"/>
    </t:zone>
    <t:actionlink t:id="generateCode" zone="myZone">Generate</t:actionlink>
</form>

和Page.java

And Page.java

Object onActionFromGenerateCode() {
    return myZone.body();
}

单击生成"链接时,挂毯会引发异常.不要让我更新表单内的区域:

When click in "Generate" link, tapestry throws an exception. Don't let me update a zone inside a form:

java.lang.RuntimeException
The component must be enclosed by a Form component.

如何更新此输入字段?

谢谢

推荐答案

通过ajax更改表单片段变得很棘手,因为

Changing pieces of a form via ajax gets tricky as a FormSupport instance must be available on the Environment in the serverside event.

可能更容易:

  1. 将整个表单放在一个区域中,然后刷新整个表单
  2. 从服务器端事件中执行一些javascript

以下是选项2的工作方式:

Here's how option 2 could work:

TML

<form>
    <input id="productCode" type="text" t:type="TextField" t:value="product.code" />
    <!-- note that async is a recent addition to eventlink in tapestry 5.4 -->
    <!-- Use a dummy zone for previous versions -->
    <t:eventlink event="generateCode" async="true">Generate</t:eventlink>
</form>

Java

@Inject JavaScriptSupport jss

void onGenerateCode() {
    String productCode = generateProductCode();
    jss.addScript("$('#productCode').val('%s');", productCode); // assuming jquery
}

注意,可以通过ajax动态更改表单,就像我说的那样,这很棘手.示例包括 AjaxFormLoop

Note, it is possible to dynamically change a form via ajax, it's just tricky as I said. Examples include AjaxFormLoop and the FormInjector

这篇关于如何更新&lt; input/&gt;通过AJAX请求在挂毯中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆