2014-09-22 193 views
2

當輸入丟了,我用:Struts2的Autocompleter - 提交按鈕被點擊

http://struts.apache.org/release/2.1.x/docs/autocompleter.html

,問題是:

當鍵入:

「球」

自動完成找到「Balloon」

and in the textf在我看到「氣球」並在下面的列表中還有「氣球」。當我點擊監視器上的其他任何地方時,它會在文本框中顯示「氣球」。但是當我點擊提交時,我從文本框中讀取「球」。爲什麼?如何?

問候, Rhodarus

回答

2

首先,不使用Struts Ajax的標籤了,because

道場插件已經過時

道場插件將Struts的被棄用2.1

如果你a現在重新啓動,你可以使用raw Dojo(今天在1.10版本,而不是Struts2附帶的舊的,buggy 0.4.x版本;但它可能會更容易遷移到jQuery,尤其是在Struts2-jQuery-plugin的幫助下。

你可以在Widgets菜單下看到它的Autocompleter的例子。

順便說一句,如果出於某種原因想要堅持使用舊的集成Dojo版本(例如,在處理大型和遺留項目時只進行最小限度更正),請確保正確使用它:返回有效的Json對象,發佈和訂閱權的議題等。如圖所示的例子從the documentation

<sx:autocompleter name="autocompleter1" href="%{jsonList}"/> 



<s:autocompleter name="test" list="{'apple','banana','grape','pear'}" autoComplete="false"/> 

<sx:autocompleter name="mvc" href="%{jsonList}" loadOnTextChange="true" loadMinimumCount="3"/> 

在autocompleter輸入的文本作爲參數傳遞給在「HREF」中指定的URL傳遞,像(文本是「struts」):http://host/example/myaction.do?mvc=struts

<form id="selectForm"> 
     <sx:autocompleter name="select" list="{'fruits','colors'}" valueNotifyTopics="/changed" /> 
</form> 
<sx:autocompleter href="%{jsonList}" formId="selectForm" listenTopics="/changed"/> 



<sx:autocompleter href="%{jsonList}" id="auto"/> 
<script type="text/javascript"> 
    function getValues() { 
     var autoCompleter = dojo.widget.byId("auto"); 

     //key (in the states example above, "AL") 
     var key = autoCompleter.getSelectedKey(); 
     alert(key); 

     //value (in the states example above, "Alabama") 
     var value = autoCompleter.getSelectedValue(); 
     alert(value); 

     //text currently on the textbox (anything the user typed) 
     var text = autoCompleter.getText(); 
     alert(text); 
    } 

    function setValues() { 
     var autoCompleter = dojo.widget.byId("auto"); 

     //key (key will be set to "AL" and value to "Alabama") 
     autoCompleter.setSelectedKey("AL"); 

     //value (key will be set to "AL" and value to "Alabama") 
     autoCompleter.setAllValues("AL", "Alabama"); 
    } 
</script> 



<script type="text/javascript"> 
dojo.event.topic.subscribe("/before", function(event, widget){ 
    alert('inside a topic event. before request'); 
    //event: set event.cancel = true, to cancel request 
    //widget: widget that published the topic 
}); 
</script>   

<sx:autocompleter beforeNotifyTopics="/before" href="%{#ajaxTest} /> 



<script type="text/javascript"> 
dojo.event.topic.subscribe("/after", function(data, request, widget){ 
    alert('inside a topic event. after request'); 
    //data : JavaScript object from parsing response 
    //request: XMLHttpRequest object 
    //widget: widget that published the topic 
}); 
</script>   

<sx:autocompleter afterNotifyTopics="/after" href="%{#ajaxTest}" /> 



<script type="text/javascript"> 
dojo.event.topic.subscribe("/error", function(error, request, widget){ 
    alert('inside a topic event. on error'); 
    //error : error object (error.message has the error message) 
    //request: XMLHttpRequest object 
    //widget: widget that published the topic 
}); 
</script> 

<sx:autocompleter errorNotifyTopics="/error" href="%{#ajaxTest}" /> 



<script type="text/javascript"> 
dojo.event.topic.subscribe("/value", function(value, key, text, widget){ 
    alert('inside a topic event. after value changed'); 
    //value : selected value (like "Florida" in example above) 
    //key: selected key (like "FL" in example above) 
    //text: text typed into textbox 
    //widget: widget that published the topic 
}); 
</script> 

<sx:autocompleter valueNotifyTopics="/value" href="%{#ajaxTest}" />