【问题标题】:How to Open BootStrap Modal Popup using a ShortCut Key如何使用快捷键打开 BootStrap 模式弹出窗口
【发布时间】:2013-05-09 21:58:07
【问题描述】:

我在我的项目中使用了Twitter Bootstrap 模态弹出对话框;

// when this button is click, the dialog is open    
<a type="button" class="btn" style="width:100%;" href="#test_modal" data-toggle="modal">Add Image</a>

如您所见,当点击上面的按钮按钮时,它会打开以下对话框;

<div class="modal fade" id="test_modal">
  <div class="modal-header">
    <a class="close" data-dismiss="modal">&times;</a>
    <h3>Modal Header</h3>
  </div>
  <div class="modal-body">
    <p>Test Modal</p>
  </div>
  <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Close</a>
    <a href="#" class="btn btn-primary">Save Changes</a>
  </div>
</div>

但是,我想为它分配一个快捷键。例如,当有人按Ctrl+Shift+L 时,我想打开上面的对话框。我不需要按下按钮。
如何在 jQuery 中实现上述功能?

【问题讨论】:

    标签: jquery hotkeys


    【解决方案1】:

    试试这个:点击ctrl+m;当点击 ctrl+m 时显示模态。

    $(document).on('keydown', function ( e ) {
        // You may replace `m` with whatever key you want
        if ((e.metaKey || e.ctrlKey) && ( String.fromCharCode(e.which).toLowerCase() === 'm') ) {
    		$("#exampleModal").modal('show');
        }
    });
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
    
        <link rel="stylesheet" media="print" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    <!-- Button trigger modal -->
    <!-- Modal -->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            ...
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>

    【讨论】:

      【解决方案2】:

      你可以这样试试

      $(document).keydown(function(evt){
          if (evt.keyCode==108 && (evt.ctrlKey) && (evt.shiftKey)){
              evt.preventDefault();
              $('#yourModal').modal('show');
          }
      });
      

      【讨论】:

      • @DotNetDreamer 你的意思是它不起作用,要清楚具体
      • 我正在按键,我把alert("working");也在里面,我没有看到任何错误,也没有工作
      猜你喜欢
      • 2016-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多