简体   繁体   中英

Detect change of value of struts2 autocompleter tag using javascript or jquery

I am working on Struts 2 application. In my application, I need to use Struts 2 autocompleter tag. For that I have used struts2-dojo-plugin-2.3.1.2.jar jar file. I need to fetch the value from autocompleter once the value changes. I tried using onchange event but it was not working. 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. You need to use 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('&lt;b&gt;'+message+'&lt;/b&gt;');
     });
   </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:

<%@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. See autocompleter doc here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM