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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用