简体   繁体   English

自动完成没有搜索结果

[英]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: 我知道ui-autocomplete显示的跨度是这样的:

<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. 当我在输入中使用onChange事件时,我需要知道搜索的结果。

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 我的HTML代码是这样的

    <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: 假设您的跨度未显示时将显示“ 0结果...”,这将对您有效:

Add an ID to your span tag like id="searchid" 向您的范围标记添加一个ID,例如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. 编辑:由于您不能向跨度添加ID,因此字符串的开头并不总是有数字。 Add the ID to the div tag then try using this: 将ID添加到div标签,然后尝试使用此标签:

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 http://api.jqueryui.com/autocomplete/#event-response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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