如何更新< input/>通过AJAX请求在挂毯中的字段 [英] How can update an <input/> field in tapestry by AJAX request
问题描述
我有一个用挂毯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.
可能更容易:
- 将整个表单放在一个区域中,然后刷新整个表单
- 从服务器端事件中执行一些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
这篇关于如何更新< input/>通过AJAX请求在挂毯中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!