【问题标题】:jQueryUI autocomplete in Bootstrap modal displaying underneath modalBootstrap 模态中的 jQueryUI 自动完成显示在模态下方
【发布时间】:2014-05-02 12:05:24
【问题描述】:

我在 Bootstrap 模式窗口中有一个 jQueryUI 自动完成功能。当我在页面中正常运行它时,它工作正常,但是当我尝试将它添加到模式中时,列表出现在模式的后面,而不是前面。

我尝试了以下变体但没有运气:

$("#myModal").css("z-index", "1500"); 
$("tags'.$fieldname.'").css("z-index", "5000");

在模态中:

data-backdrop="false"

这里是来源:

<!-- RFQ Modal -->
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" style="z-index:2000;">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>

      <div class="modal-body" data-backdrop="false"  >

      <script>
      $(function ps2() {        

      var availableTagsps2 = [
      {label:"3rd It Tech (IS# 20)", value:20},{label:"SeaBreeze Computers (IS# 14)", value:14},{label:"SeaBreeze Computers (IS# 14)", value:14},{label:"SeaBreeze Computers (IS# 14)", value:14},{label:"SeaBreeze Computers (IS# 14)", value:14},{label:"SeaBreeze Computers (IS# 14)", value:14},{label:"SeaBreeze Computers (IS# 14)", value:14} 

      ];

      $("#tagsps2").autocomplete({
           source: availableTagsps2

      });

  $( "#tagsps2" ).autocomplete({
            source: availableTagsps2,
            select: function ps2(event, ui) {
            var selectedObj = ui.item;
            $(this).val(selectedObj.label);
            $('input[name="ps2"]').val(selectedObj.value);

            return false;
        }

    });
/* $("#myModal").css("z-index","6000"); 
$("#tagsps2").css("z-index","20000");*/
  });

  </script>

  <input type="text" id="tagsps2"  required  AutoComplete="off" placeholder="Search..." 
  class="inputMed inline form-control input-med"  />
  <input type="hidden" name="ps2" value=""  />

        <div class="row" >
              <div class="col-md-6">
              <form role="form">
                  <div class="form-group" >

                    <!-- <input type="text" class="form-control" placeholder="Select client" required> -->
                  </div>

                  <button type="submit" class="btn btn-primary btn-lg">Submit</button>
                </form>
              </div>

              <div class="col-md-6">

                    <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
                    <a href="dashboard.php?vrfq" class="light">View Sent RFQ's</a>
                    </button>
                </div>

        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>

          </div>
        </div>
      </div>
    </div>
<!-- end modal -->

    <a href="#" data-toggle="modal" data-target="#myModal"><div class="RightCounter">&    nbsp;</div><h2>RFQ</h2></a>

【问题讨论】:

  • 你能创建一个bootply.com 来演示这个问题吗?我无法使用您的来源让自动完成功能正常工作。

标签: jquery twitter-bootstrap autocomplete jqmodal


【解决方案1】:
$("tags'.$fieldname.'").css("zIndex", 5000); 

【讨论】:

    【解决方案2】:

    我遇到了类似的问题。尝试添加以下内容:

    在标签的输入字段下方,添加一个带有id="tags_container"的div 像这样:

    <input type="text" id="tagsps2"  required  AutoComplete="off" placeholder="Search..." class="inputMed inline form-control input-med"  />
    <div id="container_tags">
    </div>
    

    然后添加以下css:

    .ui-autocomplete {
        position: absolute;
    }
    
    #container_tags {
        display: block; 
        position:relative
    }
    

    这应该可以解决您的对齐问题。

    【讨论】:

      【解决方案3】:

      使用 Bootstrap 3.1.1,我发现只需添加以下 CSS 即可让自动完成功能正常工作。

      .ui-autocomplete {
          z-index: 5000;
      }
      

      ui-autocomplete 类附加到 UL,该 UL 在内部用于构造自动完成。通过给它一个疯狂的高 z-index,您可以有效地确保它将被布置在页面中的所有其他内容之上,包括模式对话框。

      【讨论】:

      • 有时我想如果没有 SO 和像你这样的好心人,生活会是怎样的……非常感谢!
      • 最简单的解决方案,这可能被接受为答案
      【解决方案4】:

      使用 appendTo 选项代替 css 修改: http://api.jqueryui.com/1.10/autocomplete/#option-appendTo

      【讨论】:

      • 这对我有用,而其他答案却没有。为了更清楚,我在输入下方直接添加了
        ,然后在 jquery ui 自动完成的 JavaScript 声明中添加了 appendTo: "#autocompleteAppendToMe"。这会使菜单从您放置的那个 div 中弹出。
      【解决方案5】:

      jQuery 自动完成为此目的有一个内置的 CSS 类。只需将“ui-front”类添加到 Bootstrap 模式中的容器元素即可。

      jQuery Autocomplete docs

      .. 将检查输入字段的父项的类 用户界面前。如果找到具有 ui-front 类的元素,则菜单 将附加到该元素

      【讨论】:

      • 这比设置 z-index 要好得多。
      【解决方案6】:

      我需要 glastonbridge 和 bsandhu 的两个答案才能完成这项工作,因为“ui-front”类只添加了 100 的 z-index。

      此外,“ui-front”类需要位于立即包装我的输入控件的 div 上以获得一致的结果。

      我正在使用 BS 3.3.7、jQ 3.1.1 和 jQUI 1.12.1。

      【讨论】:

        【解决方案7】:

        什么对我有用:

        <input type="text" id="country"  class="form-control" onclick="data()" />
        

        然后在javascript中:

                var countries = [
            { value: 'Andorra', data: 'AD' },
            { value: 'Zimbabwe', data: 'ZZ' }
            ]; 
        
             function data(){
            $('#country').autocomplete({
            lookup: countries,
            onSelect: function (suggestion) {
                alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
            }
           });
        
           }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-31
          • 1970-01-01
          • 2021-08-04
          • 2020-06-01
          相关资源
          最近更新 更多