如何在自动完成中获取所选项目的值 [英] how to get value of selected item in autocomplete

查看:32
本文介绍了如何在自动完成中获取所选项目的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个来自 http://jqueryui.com/autocomplete/ 的代码,它确实很好用,但是我找不到在文本视图中获取所选项目值的方法我尝试过类似的操作,但它不起作用

<!doctype html><html lang="zh-cn"><头><meta charset="utf-8"/><title>jQuery UI 自动完成 - 默认功能</title><link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"/><script src="http://code.jquery.com/jquery-1.9.1.js"></script><script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script><link rel="stylesheet" href="/resources/demos/style.css"/><脚本>$(函数(){var availableTags = ["动作脚本","AppleScript","阿普",基本的",C","C++","Clojure","COBOL","冷聚变","二郎","Fortran",格调","哈斯克尔","爪哇","JavaScript","Lisp","珀尔","PHP",Python",红宝石","斯卡拉",方案"];$( "#tags" ).autocomplete({来源:availableTags});});<脚本>$(document).ready(function () {$('#tags').change(function () {$('#tagsname').html('你选择了:' + this.value);}).改变();});<身体><div class="ui-widget"><label for="tags">标签:</label><input id="tags"/><div id="标签名"></div>

解决方案

当自动完成更改一个值时,它会触发一个 autocompletechange 事件,而不是 change 事件

$(document).ready(function () {$('#tags').on('autocompletechange change', function () {$('#tagsname').html('你选择了:' + this.value);}).改变();});

演示:小提琴

另一种解决方案是使用select 事件,因为只有触发change事件当输入模糊时

$(document).ready(function () {$('#tags').on('change', function () {$('#tagsname').html('你选择了:' + this.value);}).改变();$('#tags').on('autocompleteselect', function (e, ui) {$('#tagsname').html('您选择了:' + ui.item.value);});});

演示:小提琴

i have here a code from http://jqueryui.com/autocomplete/ it works really good but i cant find a way to get the value of selected item in the text view i tried something like this but its not working

<script>
$(document).ready(function () {
    $('#tags').change(function () {
        $('#tagsname').html('You selected: ' + this.value);
    }).change();
});
</script>

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>jQuery UI Autocomplete - Default functionality</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <script>
  $(function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
  </script>
<script>
$(document).ready(function () {
    $('#tags').change(function () {
        $('#tagsname').html('You selected: ' + this.value);
    }).change();
});
</script>
</head>
<body>

<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags" />
  <div id="tagsname"></div>
</div>


</body>
</html>

解决方案

When autocomplete changes a value, it fires a autocompletechange event, not the change event

$(document).ready(function () {
    $('#tags').on('autocompletechange change', function () {
        $('#tagsname').html('You selected: ' + this.value);
    }).change();
});

Demo: Fiddle

Another solution is to use select event, because the change event is triggered only when the input is blurred

$(document).ready(function () {
    $('#tags').on('change', function () {
        $('#tagsname').html('You selected: ' + this.value);
    }).change();
    $('#tags').on('autocompleteselect', function (e, ui) {
        $('#tagsname').html('You selected: ' + ui.item.value);
    });
});

Demo: Fiddle

这篇关于如何在自动完成中获取所选项目的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆