簡體   English   中英

使用 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>

我如何實現這一目標。 幫我解決這個問題。

不推薦使用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('&lt;b&gt;'+message+'&lt;/b&gt;');
     });
   </script>

  </body>
</html>

您可能無法將 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>

sx:autocompleter選擇值時,將發布主題。 請參閱此處的autocompleter文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM