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

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

问题描述

我这里有来自 http://jqueryui.com/autocomplete/一个code 它的作品真的很好,但我不能找到一种方式来获得在文本视图我想这样的事情,但所选项目的价值它不工作

 <脚本>
$(文件)。就绪(函数(){
    $('#标签')。改变(函数(){
        $('#tagsname')的html(您已选择:'+ THIS.VALUE);
    })。更改();
});
< / SCRIPT><!DOCTYPE HTML>< HTML LANG =ENGT&;
< HEAD>
  <间的charset =UTF-8/>
  <标题> jQuery用户界面自动完成 - 默认功能和LT; /标题>
  <链接rel =stylesheet属性HREF =HTTP://$c$c.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css/>
  &所述; SCRIPT SRC =HTTP://$c$c.jquery.com/jquery-1.9.1.js>&下; /脚本>
  &所述; SCRIPT SRC =HTTP://$c$c.jquery.com/ui/1.10.3/jquery-ui.js>&下; /脚本>
  <链接rel =stylesheet属性HREF =/资源/演示/ style.css文件/>
  <脚本>
  $(函数(){
    VAR availableTags = [
      动作,
      AppleScript的
      ASP
      BASIC,
      C,
      C ++,
      Clojure的
      COBOL
      ColdFusion的
      二郎
      FORTRAN,
      常规
      哈斯克尔
      Java的,
      JavaScript的
      Lisp的,
      Perl的
      PHP
      蟒蛇,
      红宝石,
      斯卡拉
      方案
    ];
    $(#tags).autocomplete({
      来源:availableTags
    });
  });
  < / SCRIPT>
<脚本>
$(文件)。就绪(函数(){
    $('#标签')。改变(函数(){
        $('#tagsname')的html(您已选择:'+ THIS.VALUE);
    })。更改();
});
< / SCRIPT>
< /头>
<身体GT;< D​​IV CLASS =UI窗口小部件>
  <标签=标签>标签:LT; /标签>
  <输入ID =标签/>
  < D​​IV ID =tagsname>< / DIV>
< / DIV>
< /身体GT;
< / HTML>


解决方案

当自动完成变化的值,它触发一个 autocompletechange 事件,而不是改变事件

  $(文件)。就绪(函数(){
    $('#标签')。在('autocompletechange变化,函数(){
        $('#tagsname')的html(您已选择:'+ THIS.VALUE);
    })。更改();
});

演示:小提琴

另一个解决方案是使用选择的事件,因为只有当输入是模糊的变化事件被触发

  $(文件)。就绪(函数(){
    $('#标签')。在('变化',函数(){
        $('#tagsname')的html(您已选择:'+ THIS.VALUE);
    })。更改();
    $('#标签')。在('autocompleteselect',函数(即,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天全站免登陆