Как открыть модальное всплывающее окно BootStrap с помощью клавиши быстрого доступа

Я использовал 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?

2 ответа

Попробуйте это: нажмите 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>

Ты можешь попробовать вот так

$(document).keydown(function(evt){
    if (evt.keyCode==108 && (evt.ctrlKey) && (evt.shiftKey)){
        evt.preventDefault();
        $('#yourModal').modal('show');
    }
});
Другие вопросы по тегам