简体   繁体   English

Jquery 自动完成小部件实现

[英]Jquery Autocomplete widget implementation

I am trying to convert a normal field to autocomplete and making a ajax call to get the data in JSON and then set it to that autocomplete.我正在尝试将普通字段转换为自动完成并进行 ajax 调用以获取 JSON 中的数据,然后将其设置为该自动完成。

I do not know much on JQUERY, I spent around 5-6 hours just to know I have to initialize before using any function on the auto complete field.我对 JQUERY 了解不多,我花了大约 5-6 个小时才知道我必须在使用自动完成字段上的任何函数之前进行初始化。

What I have done so far I managed to initialize and convert my text field to autocomplete and checked that using the inspect option it shows autocomplete , and also am able to make the ajax call which is pulling the data verified that using f12 network option.But it does not show up as a type ahead in my autocomplete option.到目前为止所做的我设法初始化并将我的文本字段转换为自动完成并使用检查选项检查它显示自动完成,并且还能够进行 ajax 调用,该调用正在拉取数据验证使用 f12 网络选项。但是它不会在我的自动完成选项中显示为提前类型。

HTML HTML

<div id ="myName">
    <table class="some_class">
        <tbody>
            <tr>
                <td >
                    <label>NAMES</label>
                </td>
            </tr>
            <tr>
                <td >
                    <input type="text" id="nameText" />
                </td>
            </tr>
        </tbody>
    </table>            
</div>

initialization part初始化部分

    myName.on('valueChange',function (value){
    $("#nameText").autocomplete({
       appendTo:"#nameText",
       source:function(event,ui){
    var name=myName.value();
    $.ajax({
    url: "www.getmeSomeData.com/query/name:"+name,
    type:"GET".
    dataType:"json",
    success:function(result){
    //set this result in autocomplete which I am not sure how to do
    }
    });
    },minLength:1});
    });

$("#nameText").focus(function(even,ui){
$((this).data("uiAutocomplete").search$(this).val());
});

PROBLEM问题

1.The autocomplete does not show any result even though from ajax call i see json data coming. 1.自动完成不显示任何结果,即使从 ajax 调用我看到 json 数据来了。 2.The value change starts only after i type abc and then move the cursor somewhere else and then click it back,before that nothing invokes.Where as what is expected is as soon as I type a or ab or abc it should make ajax call and pull the data show in autocomplete dropdown. 2.只有在我输入 abc 然后将光标移到其他地方然后单击它之后才开始更改值并将数据显示在自动完成下拉列表中。

Can someone please help?有人可以帮忙吗? I did not come here without researched but I think i tried a lot of things and nothing worked so am totally confused.Kindly help me, i have spent around 2 days on this.我没有经过研究就来到这里,但我想我尝试了很多东西,但没有任何效果,所以我完全感到困惑。请帮助我,我已经花了大约 2 天的时间。

Thanks in advance.提前致谢。

I finally figured out what was the problem in my code.I actually was not able to add option to my input autocomplete.To make it work I needed to update my html with我终于弄清楚我的代码中的问题是什么。我实际上无法为我的输入自动完成添加选项。为了使它工作,我需要更新我的 html

HTML just replace <input class="nameClass" type="text" id="nameText" /> HTML只需替换<input class="nameClass" type="text" id="nameText" />

And the jquery part needed updates, the above was just a very novice attempt.而jquery部分需要更新,以上只是一个非常新手的尝试。

 1. I should have used $.each($(".nameClass"), function(index, item) {
 2. and then $(item).autocomplete
 3. Also in source should have used source:function(request,response)
 4. In the ajax call request.term (which will take whatever you input in the autocomplete field where as my method was invoking the ajax call only after tab out.
 5. Map the data of response response($.map(data.data, function(item){
 6. Write a select callback function to make anything happen after i click on any entry in the typeahead
 7.data("ui-autocomplete")._renderItem = function (ul, item) { >To show the data in a formatted way after the ajax call.

INITIALIZATION初始化

 $.each($(".nameClass"), function(index, item) {
    $(item).autocomplete({
       source:function(request,response){
    $.ajax({
    url: "www.getmeSomeData.com/query/name:"+request.term,
    type:"GET".
    dataType:"json",


success:function(result){
    //set this result in autocomplete which I am not sure how to do
      response($.map(data.data, function(item){
                  return{
                  value:item.somethigncomingfromJson //will set into the field
                  data:item
                }}))}}
     });
    } ,minLength :2,
    select:function(event,ui){
   //do something on select of a row in the autocomplete dropdown
    }}).data("ui-autocomplete")._renderItem=function(ul,item){
   return $("format in which you want to see the data").appendTo(ul);
   });
  }

No other event is required.不需要其他事件。

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

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