weixin_33724059 2020-03-26 16:47 采纳率: 0%
浏览 34

如何用 jQuery 自动完成加载?

我正在使用jquery自动完成插件。一切运行正常,但是如果下拉列表中只有一项,我就会触发select函数,以便自动填充字段。 基本上这些都是在页面加载后进行设置的。

这是我做的一个示例:
$('#auto-complete').autocomplete({
    source:function(){//get data here},
    select:function(e,ui){
     //setting fields here: 
      $('#user').val("");
    },
   focus:function(){}

});

我已经添加了响应功能,但即使这样,似乎也需要先在自动完成字段中进行更改。

  • 写回答

1条回答 默认 最新

  • weixin_33726313 2020-03-28 17:25
    关注

    You can use the Response callback:

    Triggered after a search completes, before the menu is shown. Useful for local manipulation of suggestion data, where a custom source option callback is not required. This event is always triggered when a search completes, even if the menu will not be shown because there are no results or the Autocomplete is disabled.

    Source: https://api.jqueryui.com/autocomplete/#event-response

    $(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"
        ];
        */
    
      var availableTags = [
        "ActionScript"
      ];
    
    
      $("#tags").autocomplete({
        source: availableTags,
        minLength: 0,
        response: function(e, ui) {
          if (ui.content.length == 1) {
            $(this).val(ui.content[0].value);
            $(this).autocomplete("close");
          }
        }
      });
    
      $("#tags").autocomplete("search", "");
    });
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.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>
    
    <div class="ui-widget">
      <label for="tags">Tags: </label>
      <input id="tags">
    </div>

    This is helpful is testing the the results after your Custom Source is called. I suspect you will need to update it a bit.

    Alternately, you can test the results before you send the results to response() and if they are only 1, set the value at that time.

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧