【问题标题】:Move position of jQueryUI autocomplete移动 jQueryUI 自动完成的位置
【发布时间】:2016-01-31 17:37:07
【问题描述】:

如何将 jQueryUI 自动完成显示列表的位置向下移动一点?

我可以使用var ul=$("#field").autocomplete().autocomplete('widget');来选择它。

但是,在 ul 上使用 offset() 不起作用,因为在打开小部件时会定位 ul

请参阅http://jsfiddle.net/tu0usot8/

【问题讨论】:

    标签: jquery css jquery-ui jquery-ui-autocomplete


    【解决方案1】:

    您可以使用“位置选项”字段,如下面的 sn-p,我将自动完成向右移动了 100。

    var ul=$("#field").autocomplete({ 来源:country_starting_with_A, 最小长度:1, 位置:{我的:“左上+20”,在:“左下”}, 选择:函数(事件,用户界面){ ..................................................

    详情可以看position

    $(function () {
      var countries_starting_with_A = [
        {
          "id": "1",
          "value": "Afghanistan",
          "label": "Afghanistan"
        },
        {
          "id": "17",
          "value": "Albania",
          "label": "Albania"
        },
        {
          "id": "18",
          "value": "Algeria",
          "label": "Algeria"
        },
        {
          "id": "20",
          "value": "American Samoa",
          "label": "American Samoa"
        },
        {
          "id": "22",
          "value": "Andorra",
          "label": "Andorra"
        },
        {
          "id": "10",
          "value": "Angola",
          "label": "Angola"
        },
        {
          "id": "11",
          "value": "Anguilla",
          "label": "Anguilla"
        },
        {
          "id": "23",
          "value": "Antarctica",
          "label": "Antarctica"
        },
        {
          "id": "24",
          "value": "Antigua and Barbuda",
          "label": "Antigua and Barbuda"
        },
        {
          "id": "25",
          "value": "Argentina",
          "label": "Argentina"
        },
        {
          "id": "26",
          "value": "Armenia",
          "label": "Armenia"
        },
        {
          "id": "27",
          "value": "Aruba",
          "label": "Aruba"
        },
        {
          "id": "28",
          "value": "Australia",
          "label": "Australia"
        },
        {
          "id": "29",
          "value": "Austria",
          "label": "Austria"
        },
        {
          "id": "12",
          "value": "Azerbaijan",
          "label": "Azerbaijan"
        }
      ];
    
      var ul=$("#field").autocomplete({
        source: countries_starting_with_A,
        minLength: 1,
        position: { my : "left top+20", at: "left bottom" },
        select: function(event, ui) {
          // feed hidden id field
          $("#field_id").val(ui.item.id);
          // update number of returned rows
          $('#results_count').html('');
        },
        open: function(event, ui) {
          // update number of returned rows
          var len = $('.ui-autocomplete > li').length;
          $('#results_count').html('(#' + len + ')');
        },
        close: function(event, ui) {
          // update number of returned rows
          $('#results_count').html('');
        },
        // mustMatch implementation
        change: function (event, ui) {
          if (ui.item === null) {
            $(this).val('');
            $('#field_id').val('');
          }
        }
      }).autocomplete('widget');
      console.log(ul)
    
      // mustMatch (no value) implementation
      $("#field").focusout(function() {
        if ($("#field").val() === '') {
          $('#field_id').val('');
        }
      });
    });
    <link href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.min.css" rel="stylesheet"/>
    <script src="http://code.jquery.com/jquery-1.11.3.js"></script>
    <script src="http://code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
    
    
    <label for="field_id">ID</label>
    <input id="field_id" type="text" style="width: 40px;">
    <label for="field">Countries (starting with A)</label>
    <input id="field" type="text" style="width: 200px;">
    &nbsp;&nbsp;&nbsp;
    <span id="results_count"></span>
    <br><br>
    <input type="button" value="OK">

    【讨论】:

    • 这些似乎是更“正确”的解决方案,但是,目前的实现显示它向右移动而不是向下移动。我们能做相对运动吗?
    • 我更新了 sn-p,现在就像你对偏移量所做的那样,我对位置重复了。
    • 啊,你明白了!介意解释为什么{ my : "left top+20", at: "left bottom" } 会这样做吗?
    • 是的,它做了你试图用偏移量做的事情,因为元素的位置现在是顶部+20,而不是你输入的“at”,它必须在左侧和底部位置。
    • 我的偏移解决方案也很有效。我只是觉得您的解决方案是更正式的方法。感谢您的帮助!
    【解决方案2】:

    不确定它是否是最好的,但一种选择是:

    open: function(event, ui) {
          var ul = $(this).autocomplete('widget'),offset=ul.offset();
          ul.offset({top:offset.top+20}) //Move 20px down
        },
    

    【讨论】:

      【解决方案3】:

      jQueryUI 插件更改了自动完成元素的样式,因此它将位于文本字段旁边。 因为 style 属性只能使用 !important 关键字来覆盖,并且不建议更改插件的代码来绕过它,所以可以使用以下 CSS:

      .ui-autocomplete {
        top: 100px !important;
      }
      

      【讨论】:

      • 嗨 Eburge,您知道自动完成列表的位置是否始终相同,无论主题等如何?
      猜你喜欢
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      相关资源
      最近更新 更多