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

时间:2017-03-17 06:16:09

标签: php jquery autocomplete

如何取值而不是取标签

我得到了名字,但我希望有像" ActionScript"值为1" AppleScript"价值是2

除了指导我添加依赖自动选择,下面是我的代码

<?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 个答案:

答案 0 :(得分:3)

您可以定义选择回调,获取所选值,获取源列表并获取列表中所选项目的索引:

&#13;
&#13;
 $( 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));
          }
        });
      } );
&#13;
<!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>
&#13;
&#13;
&#13;