JQuery自动完成

时间:2013-01-30 14:27:57

标签: asp.net-mvc jquery autocomplete

我的jQuery自动填充字段存在问题。它有些奇怪。

这是我的自动填充字段和脚本。我的mvc函数的响应运行正常。下拉列表是可见条目。但是当我试图选择一个项目时,结果列表就会消失。有没有人有想法?

 <div class="ui-widget">
    <input id="newPlayerName" type="text" name="newPlayerName" onkeyup="checkRegistration()" />
 </div>

代码:

<script type="text/javascript">
  $(function () {
      $('#newPlayerName').autocomplete({
          source: function (request, response) {
              $.ajax({
                  url: '/Trainer/Search',
                  data: {
                      searchTerm: request.term
                  },
                  dataType: 'json',
                  type: 'POST',
                  minLength: 1,

                  success: function (data) {
                      response(data);
                  }
              });
          },
          select: function (event, ui) {
              checkRegistration(ui.item.value);
          },
          focus: function (event, ui) {
              event.preventDefault();
              $("#newPlayerName").val(ui.item.label);
          }
      });
  });
</script>

啊......这是我正在使用的jquery脚本......

<script src="/Scripts/jquery-1.9.0.js" type="text/javascript"></script>
<script src="/Scripts/jquery-ui-1.10.0.custom.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery-ui-1.10.0.custom.js" type="text/javascript"></script>

2 个答案:

答案 0 :(得分:5)

您所显示的代码似乎有一点似乎是您已经将jquery-ui脚本包含两次(缩小版和标准版)。你应该只有一个:

<script src="/Scripts/jquery-1.9.0.js" type="text/javascript"></script>
<script src="/Scripts/jquery-ui-1.10.0.custom.min.js" type="text/javascript"></script>

答案 1 :(得分:0)

&#13;
&#13;
$(function () {
  var getData = function (request, response) {
    $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "Default.aspx/GetMultiProductList",
      data: "{'term':'" + $("#txtAutoCompleteMulti").val() + "'}",
      dataType: "json",
      success: function (data) {
        response($.map(data.d, function (item) {
          if (item != null)
            return { label: item.label, title: item.value }//value: item.label,
            }))
      },
      error: function (result) {
        alert("Error");
      }
    });
  };
  var selectItem = function (event, ui) { $("#txtAutoCompleteMulti").val(ui.item.value); return false; }
  $("#txtAutoCompleteMulti").autocomplete({
    source: getData,
    select: selectItem,
    _resizeMenu: function () {
      this.menu.element.outerWidth(500);
    },
    search: function (event, ui) { },
    minLength: 1,
    change: function (event, ui) {
      if (!ui.item) {
        $('#txtAutoCompleteMulti').val("")
      }
    },
    select: function (event, ui) {
      $("#txtAutoCompleteMulti").prop('title', ui.item.title)
    },
    autoFocus: true,
    delay: 500
  });
});
&#13;
 .ui-autocomplete {
            max-height: 300px;
            overflow-y: auto;
            overflow-x: hidden;
        }
        .ui-autocomplete-loading {
            background: url('img/Progress_indicator.gif') no-repeat right center;
        }
        .seachbox {
            border: 1px solid #ccc;
            border-radius: 4px;
            width: 250px;
            padding: 6px 25px 6px 6px;
            transition: border-color 0.15s ease-in-out 0s, box-shadow 0.15s ease-in-out 0s;
        }
&#13;
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script>

<div>
   <table style="width: 100%;">
     <tr>
       <td style="width: 20%">Product Name :</td>
       <td>
         <input type="text" id="txtAutoCompleteMulti" placeholder="Seach Product" class="seachbox" />
       </td>
     </tr>
  </table>
</div>
&#13;
&#13;
&#13;

c# code using webmethod