JQuery UI自动完成AJAX调用

时间:2016-10-13 16:47:16

标签: javascript jquery ajax autocomplete

我正在尝试创建一个搜索字段,该字段使用ajax调用自动填充来自数据库的建议。选择项目后,我希望它能够更新搜索字段。目前,当我在此文本框中键入内容时,它会返回一个空选择。任何帮助,将不胜感激。

这是代码,更新了推荐的更改,但现在我得到了空列表。

function autoCompleteCheckRun() {
$('#autocompleteCR')
          .autocomplete({
              minLength: 0,
              source: function (request, response) {
                  $.ajax({
                      type: "POST",
                      contentType: "application/json; charset=utf-8",
                      url: './PayInvoicesWS.asmx/GetCheckRun',
                      data: "{'description':'" + document.getElementById('autocompleteCR').value + "'}",
                      dataType: "json",
                      success: function (data) {
                          var rows = autocompleteJSONParseCode(data);
                          response(rows);
                      },
                      error: function (result) {
                          alert("Error");
                      },
                      select: function (event, ui) {
                          var checkRunData = $("#CheckRunDescription");
                          var checkRunID = $("#CheckRunID");
                          checkRunData.val(ui.item.label);
                          checkRunID.val(ui.item.value);


                      }
                  });
              }
          });
 }

function autocompleteJSONParseCode(data) {
    var rows = new Array();
    var rowData = null;
    for (var i = 0, dataLength = data.d.length; i < dataLength; i++) {
        rowData = data.d[i];
        // alert(rowData.term2+":"+rowData.term1);//uncomment to see data as it parses
        rows[i] = {
            value: rowData.CheckRunID,
            label: rowData.Description
        };
    }

    return rows;
}

div class="ui-widget">
                        <label for="autocompleteCR" id="checkRunLabel">Check Run Lookup:</label>
                        <input type="text" id="autocompleteCR" />
                    </div>

example

2 个答案:

答案 0 :(得分:2)

您错放了select功能。将其从source函数移出并平行放置到source函数。

$('#autocompleteCR')
          .autocomplete({
              minLength: 0,
              source: function (request, response) {
                  $.ajax({
                      type: "POST",
                      contentType: "application/json; charset=utf-8",
                      url: './PayInvoicesWS.asmx/GetCheckRun',
                      data: "{'description':'" + document.getElementById('autocompleteCR').value + "'}",
                      dataType: "json",
                      success: function (data) {
                          var rows = autocompleteJSONParseCode(data);
                          response(rows);
                      },
                      error: function (result) {
                          alert("Error");
                      }
                  });
              },
              select: function (event, ui) {
                          var checkRunData = $("#CheckRunDescription");
                          var checkRunID = $("#CheckRunID");
                          checkRunData.val(ui.item.label);
                          checkRunID.val(ui.item.value);


              }
          });

我为你要找的东西做了一个有效的例子:

https://jsfiddle.net/gschambial/d0g3dLvu/19/

$( "#tags" ).autocomplete({
  source: availableTags,
  select: function(event, ui) {
            alert('Label is :' + ui.item.label + ' and Value is : ' +ui.item.value);
    }
});

更改

function(el) {
return {
     CheckRunID: el.CheckRunID,
     Description: el.Description
     };
}));

function(el) {
return {
     value: el.CheckRunID,
     label: el.Description
     };
}));

而不是

checkRunData.val(ui.item.CheckRunID + ui.item.Description);

使用

checkRunData.val(ui.item.label + ui.item.value);

答案 1 :(得分:0)

这就是它的工作原理:在ajax调用的success函数内为data.d分配一个变量。还有@ gschambial关于我在哪里选择功能的建议。最初它是在源函数内部,一旦我移动到它之外,我就能够将列表中的值分配给dom元素。

function autoCompleteCheckRun() {
$('#autocompleteCR')
          .autocomplete({
        minLength: 0,
        source: function(request, response) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: './PayInvoicesWS.asmx/GetCheckRun',
                data: "{'description':'" + document.getElementById('autocompleteCR').value + "'}",
                dataType: "json",
                success: function(data) {
                    var el = data.d;
                    response($.map(el,
                        function(el) {
                            return {
                                label: el.Description,
                                value: el.CheckRunID
                            }
                        }));
                },
                error: function(result) {
                    alert("Error");
                }
             });
            },
            select: function (event, ui) {
                  $("#CheckRunDescription").val(ui.item.label);
                  $("#CheckRunID").val(ui.item.value);
                event.stopPropagation();
            }

          });
      }

观点:

 <div class="ui-widget">
                        <label for="autocompleteCR" id="checkRunLabel">Check Run Lookup:</label>
                        <input type="text" id="autocompleteCR" />
                    </div>
<div class="divTableCell">
                <label for="CheckRunDescription" id="checkRunDescriptionLabel">Description:</label>
                <input type="text" id="CheckRunDescription" style="width: 270px;" />
            </div>