简体   繁体   中英

AutoComplete no search results

I need to change the value of a hidden value, depending if the search of the autocomplete had any results. I know that the ui-autocomplete displays a span that is something like this:

<span role="status" aria-live="polite" class="ui-helper-hidden-accessible">1 result is available, use up and down arrow keys to navigate.</span>

I need to know the results of the search when I am using a onChange event in an input.

My code has to be something like this:

    function onChangeHiddenValue() {
    if(search == 'No results) --> I need to change this
        {
        document.forms['form'].elements['hiddenId'].value = cliente.id;
        }
    else
        {
        document.forms['form'].elements['hiddenId'].value = null;
        }
  }

My Html code is something like this

    <div class="form-group">
        <label for="name" class="uppercase"><fmt:message key="user.name" /></label>
        <form:input  onchange="onChangeHiddenValue();" cssClass="form-control" path="user.name" id="name"disabled="${readonly}" submitbyenter="true" />
        <form:errors path="user.id" cssClass="error-block" />
        <form:hidden path="user.id" id="userId" />
    </div>

Assuming your span will say "0 result..." when none are found this should work for you:

Add an ID to your span tag like id="searchid"

if(parseInt(document.getElementById("searchid").innerHTML.charAt(0)) > 0)

EDIT: Since you can't add an id to the span and there is not always a number at the start of the string. Add the ID to the div tag then try using this:

var searchbox=document.getElementById('searchid');
var searchresult=searchbox.getElementsByTagName('span')[0].innerHTML;
try {
    var number = parseInt(searchresult.charAt(0));
    if(number > 0)
    {
        document.forms['form'].elements['hiddenId'].value = null;
    }
    else
    {
        document.forms['form'].elements['hiddenId'].value = cliente.id;
    }
}
catch(err) {
    document.forms['form'].elements['hiddenId'].value = cliente.id;
}

You can use the response event to check if there are results and if there aren't, then you can set the value of the hidden input.

http://api.jqueryui.com/autocomplete/#event-response

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