dounaidu0204 2017-03-17 06:16
浏览 79

如何获取jquery自动完成值而不是标签? 如果我添加依赖自动完成,还指导我?

How to take values instead of taking labels

I'm getting names but I want there values like "ActionScript" value is 1 "AppleScript" value is 2

As well as guide me if I add dependence autoselect, below is my code

<?php 
if(isset($_POST) && !empty($_POST))
{
    print_r($_POST);
}
?>    
<!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>jQuery UI Autocomplete - Default functionality</title>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
      <link rel="stylesheet" href="/resources/demos/style.css">
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      <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>
    </head>
    <body>
    <form action="" method="POST"> 
    <div class="ui-widget">
      <label for="tags">Tags: </label>
      <input id="tags" name="tags">
    </div>
    <input type="submit" name="submit">
    </form>


</body>
</html>

PHPFiddle

  • 写回答

1条回答 默认 最新

  • drdyszuy488152 2017-03-17 06:50
    关注

    You can define a select callback, get the chosen value, get the source list and fetch the index of the selected item in the list:

     $( 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,
              select: function (event, ui) {
                event.preventDefault();
                var source = $( "#tags" ).autocomplete("option", "source" );
                var value = ui.item.value;
                $(this).val(source.indexOf(value)+1);
                // console.log(source.indexOf(value)+1, $(this));
              }
            });
          } );
    <!doctype html>
        <html lang="en">
        <head>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <title>jQuery UI Autocomplete - Default functionality</title>
          <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
          <link rel="stylesheet" href="/resources/demos/style.css">
          <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
          <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
        </head>
        <body>
        <form action="" method="POST"> 
        <div class="ui-widget">
          <label for="tags">Tags: </label>
          <input id="tags" name="tags">
        </div>
        <input type="submit" name="submit">
        </form>
    
    
    </body>
    </html>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序