【问题标题】:Twitter Bootstrap modal blocks text input fieldTwitter Bootstrap 模式阻止文本输入字段
【发布时间】:2013-01-25 13:04:46
【问题描述】:

我正在尝试使用模式作为弹出帮助窗口。 我将背景设置为“无”。 当模式打开(没有背景)时,“原始”页面中的输入字段无法聚焦。

其他输入类型(示例中的复选框和按钮)运行良好...

有什么想法吗?

我的代码:

<div class="container">
<!-- Button to trigger modal -->
  <form>
      <label>Input</label>
      <input type="text" placeholder="Type something…">
      <label class="checkbox">
        <input type="checkbox">Checkbox
      </label>
      <button type="submit" class="btn">Submit</button>
  </form>

  <!-- Button to trigger modal -->
  <a href="#myModal" role="button" class="btn" data-toggle="modal">Open Help...</a>

  <!-- Modal -->
  <div id="myModal" class="modal hide" data-backdrop="" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
      <h3 id="myModalLabel">Modal header</h3>
    </div>
    <div class="modal-body">
      <p>One fine body…</p>
    </div>
    <div class="modal-footer">
      <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    </div>
  </div>
</div>

使弹出窗口可拖动的简单 Javascript:

$('#myModal').draggable();

所有这些都在 JSFiddle 上...

http://jsfiddle.net/Jxdd8/3/

【问题讨论】:

  • 很高兴我找到了这个问题。很难用谷歌搜索“制作模态而不是模态”:)
  • 您应该将 Eric 的回答标记为已接受。
  • 在模态窗口中使用 select2 或类似组件时,这是一个真正的问题。 Select2 创建一个不是模态后代的输入,因此 Eric Freese 的答案解决了这个问题。希望这个 cmets 可以帮助某人!

标签: twitter-bootstrap modal-dialog


【解决方案1】:

听起来模态不是解决您的问题的正确方法。

根据定义,模态对话框不应允许用户与其下方的任何内容进行交互。

在用户界面设计中,模态窗口是一个子窗口 要求用户在返回操作之前与其交互 父应用程序,从而阻止 应用程序主窗口。

-http://en.wikipedia.org/wiki/Modal_window

话虽如此,有一种方法可以绕过它。引导sets up an event listener 以从其他所有内容中窃取焦点,这就是阻止您编辑文本输入的原因。其他按钮之所以有效,是因为它们不需要焦点,只需要一个点击事件。

您可以侦听引导模式触发的“显示”事件,并禁用它设置的焦点侦听器。

$('#myModal').on('shown', function() {
    $(document).off('focusin.modal');
});

只是为了好玩:http://jsfiddle.net/Jxdd8/4/

【讨论】:

  • 谢谢,效果很好!我同意这违反了模态的定义,无论如何,我想要一个页面上的对话框,Bootstrap modal 非常适合重新渲染它!看起来模态窃取焦点不适用于所有输入元素!
  • 效果很好!!!很高兴我找到了这个!我希望我们可以投票选出一个被接受的答案:)
  • 感谢这个明确的答案,现在我知道什么是模态的了。我在哪里可以投票超过一个?
  • 这个答案非常适合在 Bootstrap 模式中使用 Kendo UI 网格(在注意到 @animativ 对 Bootstrap 3+ 的更新之后)。
【解决方案2】:

正如 Eric Freese 所说,“根据定义,模态对话框不应允许用户与其下方的任何内容进行交互。” 我猜你要找的可以在 Popover 中找到,它可以选择允许 html 内容http://twitter.github.com/bootstrap/javascript.html#popovers

【讨论】:

  • 弹出窗口有一种非常不同的感觉(无论如何在引导程序中)。非模态模态正是我所需要的,我很高兴找到了这个问题。
【解决方案3】:

*已解决

如果有人仍然卡在这个问题上,解决方案如下:

在 div 类 &lt;div class="modal fade" 之后插入:style="display:none;" 这将阻止模式阻塞字段,它应该可以解决问题。

【讨论】:

    【解决方案4】:

    请记住,如果您使用 Twitter Bootstrap 3,Eric Freese 的回答将不再有效 - 事件名称已更改,因此请改用以下代码:

    $('#myModal').on('shown.bs.modal', function() {
        $(document).off('focusin.modal');
    });
    

    【讨论】:

    • 我正在使用动态生成的模态。有什么方法可以完全关闭它? $(document).on('shown.bs.modal', function() { $(document).off('focusin.modal'); });试过了,没用:-(
    • 感谢您的帖子。这是最好的解决方法...我尝试了一切来解决这个问题,但归根结底,这是最轻松的方法。我有一个问题...为什么 $(document)... 有效但 $(this)... 无效?这并不重要,我只是好奇。
    【解决方案5】:

    以下方法可以解决IE11中Bootstrap v2.3.2、Kendo UI Grid 2013.3.1119.340版本的这个问题。

    关键是从模态的类中删除类“in”。不需要使用 "style='display:none;'" 因为它会导致 Kendo Grid UI 奇怪。

    修复前的html代码:

      <div class="modal hide fade in" id="YourWindow" role="dialog">
    

    修复后的html代码:

      <div class="modal hide fade" id="YourWindow" role="dialog">
           <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>            
           </div>
           <div class="modal-body" >
              <div id="YourWindowBody">
              </div>
           </div>
           <div class="modal-footer">
              <button type="button" data-dismiss="modal">Close</button>      
           </div>
      </div>
    

    同时,添加以下 javascript 行:

        $("#YourWindow").on('shown', function () {
            $(document).off('focusin.modal');
        });
    

    【讨论】:

      【解决方案6】:

      使用 Bootstrap 3.3.2 版,上述解决方案均无效。

      添加以下 CSS 就可以了

      .modal-backdrop {
          position: static; 
      }
      

      【讨论】:

        【解决方案7】:

        使用此代码结束所有 javascript 代码:

        $(document).on('focusin', function(e) {
            if ($(event.target).closest(".mce-window").length) {
                e.stopImmediatePropagation();
            }
        });
        

        jsfiddle

        【讨论】:

        • 但这适用于 bootstrap modal 中的 tinymce。
        • 唯一对我有用的解决方案。不知道为什么有些人早些时候投了反对票。
        【解决方案8】:

        请删除:

        tabindex="-1"
        

        来自

        <div id="myModal" class="modal hide" data-backdrop="" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        

        改用这个:

        <div id="myModal" class="modal hide" data-backdrop="" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        

        【讨论】:

        • 请解释为什么你认为它会解决 OP 的问题 - 这对每个人来说可能并不明显。
        • @Balázs 同意了。该解决方案对我有用,但我必须查找原因。本质上,通过说tabindex="-1",模态将所有背景inputs 设置为will not receive focuswufoo.com/html5/tabindex-attribute
        • 这对我展示 Kendo Dropdown 元素很有用。上面显示的事件答案对我来说根本没有触发!谢谢!
        【解决方案9】:

        对我来说,问题是我使用的是 jquery 3.2.1,所以我将其更改为 2.1.3,它在我们开发的以前的应用程序中使用,一切正常。可能与 jquery 和 bootstrap 兼容版本有关。

        希望对你有帮助

        【讨论】:

          【解决方案10】:

          以下对我有用:

          $('#myModal').on("show.bs.modal", (ev) => {
              $('#myModal').find(".modal-content").on("mousedown", (e) => {
          
                          if ($(e.target).is("input")) {
                              //Fix for bootstrap modal being stealing focus from inputs like textbox
                              e.stopImmediatePropagation();
                          }
                      });
          });
          

          【讨论】:

            【解决方案11】:

            请删除:

            tabindex="-1"
            

            并将模态框的第一行更改为

            tabindex=""
            

            这将启用输入字段的焦点。

            【讨论】:

            • 我尝试了很多东西,最终你的解决方案奏效了。非常感谢。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-03-06
            • 1970-01-01
            • 2012-08-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多