-->

AutoCompleter working but default text is not disp

2019-08-31 05:29发布

问题:

I have an autocompleter tag in Struts 2.

I have certain operations to be done when the tag is placed on the autocompleter....

On focus, when I click the autocompleter textbox, the text Select or Type tag ends with ; will be removed.

If I process some other event, the text should reappear.

How to achieve this functionality ?

<sj:autocompleter  
           cssStyle="width:200px;" 
               href="%{#autoCompleteTagUrl}" 
     onSelectTopics="tagsAllSelectTopics" 
   onCompleteTopics="tagsAllCompleteTopics"
                 id="tags_all" 
               name="tags_all" 
           cssClass="inputText tags_all tag-textbox docTxt" 
            tooltip="true" 
              value="Select or Type tag ends with ;" 
            onfocus="if(this.value=='Select or Type tag ends with;'){this.value='';}" 
        loadingText="Loading...." 
/>

回答1:

IF <sj:autocompleter /> allows dynamic attributes (that means that you can write attributes not listed on the documentation, like <sj:autocompleter foo="bar" />), you can use the HTML5 placeholder text, working in every modern browser:

<sj:autocompleter placeholder="Select or Type tag ends with" />

This will be totally automatic, and for old browser, you can use feature detection like in this fiddle.

Stop using text for something that is not a text, but an inner label ;)



回答2:

Autocompleter does not have a placeholder attribute because autocompleter is usually meant for users to input text that fall within a provided list and this list is shown as and when the user types or if the drop down arrow is clicked(if arrow is enabled).

Here is a list of attributes supported by autocompleter.

But if want to do it like that then use the onfocusout event to put the text back if value==""

<sj:autocompleter  
       cssStyle="width:200px;" 
       href="%{#autoCompleteTagUrl}" 
       onSelectTopics="tagsAllSelectTopics" 
       onCompleteTopics="tagsAllCompleteTopics"
       id="tags_all" 
       name="tags_all" 
       cssClass="inputText tags_all tag-textbox docTxt" 
       tooltip="true" 
       value="Select or Type tag ends with ;" 
       onfocus="if(this.value=='Select or Type tag ends with;'){this.value='';}" 
       loadingText="Loading...." 
       onfocusout="if(this.value==""){this.value='Select or Type tag ends with';}
  />