使用 javascript 或 jquery 检测 struts2 自动完成标签的值变化
Detect change of value of struts2 autocompleter tag using javascript or jquery
我正在处理 Struts 2 应用程序。在我的应用程序中,我需要使用 Struts 2 autocompleter
标签。为此,我使用了 struts2-dojo-plugin-2.3.1.2.jar
jar 文件。一旦值发生变化,我需要从自动完成器中获取值。我尝试使用 onchange
事件,但它不起作用。这是我的代码:
<%@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>
我该如何实现。帮我解决这个问题。
您可能无法将 HTML 属性与 dojo 小部件一起使用,您需要使用 dojo topics 来订阅事件:
<%@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>
为 sx:autocompleter
选择值时,将发布主题。请参阅 autocompleter
文档 here。
struts2-dojo-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>
我正在处理 Struts 2 应用程序。在我的应用程序中,我需要使用 Struts 2 autocompleter
标签。为此,我使用了 struts2-dojo-plugin-2.3.1.2.jar
jar 文件。一旦值发生变化,我需要从自动完成器中获取值。我尝试使用 onchange
事件,但它不起作用。这是我的代码:
<%@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>
我该如何实现。帮我解决这个问题。
您可能无法将 HTML 属性与 dojo 小部件一起使用,您需要使用 dojo topics 来订阅事件:
<%@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>
为 sx:autocompleter
选择值时,将发布主题。请参阅 autocompleter
文档 here。
struts2-dojo-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>