Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何防止在jQueryUI自动完成列表中选择项时更新输入元素?_Jquery_Jquery Ui_Jquery Ui Autocomplete - Fatal编程技术网

如何防止在jQueryUI自动完成列表中选择项时更新输入元素?

如何防止在jQueryUI自动完成列表中选择项时更新输入元素?,jquery,jquery-ui,jquery-ui-autocomplete,Jquery,Jquery Ui,Jquery Ui Autocomplete,我有以下jQueryUI自动完成 $('#clientSearch').autocomplete({ source: function (request, response) { var url = window.apiUrl + '/clients?searchText=' + request.term; var request = $.ajax({ url: url, type: 'GET', dataType: "json",

我有以下jQueryUI自动完成

$('#clientSearch').autocomplete({
  source: function (request, response) {

    var url = window.apiUrl + '/clients?searchText=' + request.term;

    var request = $.ajax({
      url: url,
      type: 'GET',
      dataType: "json",
      cache: false,
      contentType: 'application/json; charset=utf-8'
    });

    request.done(function (clientList) {
      response($.map(clientList, function (client) {
        return {
          label: client.lastName + ', ' + client.firstInitial + '. ' + client.sex + '/' + client.age,
          value: client.id
        }
      }));
    });

    request.fail(function (jqXHR, textStatus, errorThrown) {
      response(
      {
        label: 'Error retrieving clients',
        value: null
      }
      );
    });

  },
  minLength: 2,
  select: function (event, ui) {
    event.preventDefault();
    getClient(ui.item.value);
  }
});
当按下向下箭头将焦点/突出显示到项目1时,显示自动完成列表后,用户键入搜索条件的输入元素中将显示item.value

从我所读到的内容来看,在select事件中添加event.preventDefault(如上代码所述)应该可以防止将item.value插入到输入元素中

但是,仍将插入item.value,并且在通过ENTER选择突出显示的项之前,不会命中select事件中的断点

当突出显示在自动完成列表中的项目之间移动时,我希望原始搜索文本保留在输入元素中。

请改用事件:

focus: function (event, ui) {
    event.preventDefault();
}

还值得注意的是,除了Andrew防止突出显示更改插入item.value的解决方案外,如果您希望在用户选择一个item event.preventDefault时防止插入item.value,还需要在选择事件中使用该解决方案。