[英]Detect change of value of struts2 autocompleter tag using javascript or jquery
I am working on Struts 2 application.我正在研究 Struts 2 应用程序。 In my application, I need to use Struts 2
autocompleter
tag.在我的应用程序中,我需要使用 Struts 2
autocompleter
标记。 For that I have used struts2-dojo-plugin-2.3.1.2.jar
jar file.为此,我使用了
struts2-dojo-plugin-2.3.1.2.jar
jar 文件。 I need to fetch the value from autocompleter once the value changes.一旦值发生变化,我需要从自动完成程序中获取值。 I tried using
onchange
event but it was not working.我尝试使用
onchange
事件,但它不起作用。 Here is my code:这是我的代码:
<%@taglib uri="/struts-dojo-tags" prefix="sx"%>
<html>
<head>
<script type="text/javascript">
function abc() {
var a = dojo.widget.byId("country");
var value1 = a.getSelectedValue();
document.getElementById("myText").value = value1;
}
</script>
<sx:head />
</head>
<body>
<sx:autocompleter name="country"
id="country" onchange="abc();" list="cricketNations" />
</body>
</html>
How do I achieve this.我如何实现这一目标。 Help me solve this issue.
帮我解决这个问题。
The struts2-dojo-plugin
is deprecated.不推荐使用
struts2-dojo-plugin
。 You need to use struts2-jquery-plugin
.您需要使用
struts2-jquery-plugin
。
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<html>
<head>
<sj:head jqueryui="true"/>
</head>
<body>
<div id="myText" class="result ui-widget-content ui-corner-all"></div>
<sj:autocompleter name = "country"
id = "country"
onChangeTopics = "autocompleteChange"
list = "%{cricketNations}" />
<script>
$.subscribe('autocompleteChange', function(event, data) {
var ui = event.originalEvent.ui;
var message = ui.item.value;
if (ui.item.key) {
message = '( '+ ui.item.key +' ) '+message;
}
$('#myText').html('<b>'+message+'</b>');
});
</script>
</body>
</html>
You might not be able to use HTML attributes with dojo widgets, you need to use dojo topics to subscribe for an event:您可能无法将 HTML 属性与 dojo 小部件一起使用,您需要使用dojo 主题来订阅事件:
<%@taglib uri="/struts-dojo-tags" prefix="sx"%>
<html>
<head>
<script type="text/javascript">
dojo.event.topic.subscribe("/countryName", function(value, key, text, widget){
alert('inside onchange');
document.getElementById("myText").value = value;
});
</script>
<sx:head />
</head>
<body>
<sx:autocompleter name="country" valueNotifyTopics="/countryName" list="cricketNations" />
</body>
</html>
When value is selected for sx:autocompleter
, topics will be published.为
sx:autocompleter
选择值时,将发布主题。 See autocompleter
doc here .请参阅此处的
autocompleter
文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.