[英]Struts2 JQuery how to do onchange ajax
Let's say I have sj:autocompleter like this : 假设我有这样的sj:autocompleter:
<sj:autocompleter id="idDistributor"
name="idDistributor"
list="%{idDistributorList}" label="ID
Distributor" indicator="indicator" />
when the value is changed, I'd like to pass the detail value to other text field like this : 当值改变时,我想将细节值传递给其他文本字段,如下所示:
<sj:textfield label="Nama Distributor" id="namaDistributor" name="namaDistributor" required="true"/>
where its value will be retrieved from struts back bean (FormAction). 其值将从struts back bean(FormAction)中检索。
How I could do this? 我怎么能这样做?
Really thanks in advance. 非常感谢提前。
Mr.K Mr.K
The following will send the autocompleter value to your action and set the value of #namaDistributor
with the returned string: 以下内容将autocompleter值发送到您的操作,并使用返回的字符串设置#namaDistributor
的值:
$('#idDistributor').change(function(){
// Make the ajax request
$.ajax({
url: 'path/to/back-bean.action',
data: "autocompleterValue=" + $(this).val(),
dataType: "json",
success: function(data) {
// Set the inputs from the json object
$('#namaDistributor').val(data.distributor);
$('#namaPrice').val(data.price);
}
});
});
You can read more about the $.ajax() method here . 您可以在此处阅读有关$ .ajax()方法的更多信息。 The above json example assumes that your Action is creating a json object with the following format: 上面的json示例假设您的Action正在使用以下格式创建json对象:
{"price": "123.40", "distributor": "XYZ Inc."}
You can read more about using json with Struts2 in this article . 您可以在本文中阅读有关在Struts2中使用json的更多信息。
you can do this with onSelectTopics. 你可以用onSelectTopics做到这一点。
Subscribe a topic like this: 订阅这样的主题:
<script>
$.subscribe('autocompleteSelect', function(event, data) {
var ui = event.originalEvent.ui;
$('#namaDistributor').val(ui.item.value);
});
</script>
and at it to your autocompleter 并在它自动完成
<sj:autocompleter id="idDistributor"
name="idDistributor" onSelectTopics="autocompleteSelect"
list="%{idDistributorList}" label="ID
Distributor" indicator="indicator" />
you can find an example for this in the Struts2 jQuery Plugin Showcase 你可以在Struts2 jQuery插件展示中找到一个例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.