■ サンプル
<!doctype html>
<html lang="jp">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<div class="container">
<h1>Modal Hello</h1>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#demoModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="demoModal" tabindex="-1" role="dialog" aria-labelledby="demoModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<!-- モーダル・ヘッダー -->
<div class="modal-header">
<h5 class="modal-title" id="demoModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<!-- モーダル・ボディ -->
<div class="modal-body">
Hello World!
</div>
<!-- モーダル・フッター -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">OK</button>
</div>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>
■ モーダルあれこれ
Enterキー押下でモーダルが閉じてしまう
* 以下の「現象が発生するサンプル・抜粋」のコンテンツで、 input欄内でEnterキー押下すると、モーダルが閉じてしまう現象が発生するサンプル・抜粋
・・・
<!-- モーダル・ボディ -->
<form>
<div class="modal-body">
Hello World!
<input type="text">Sample
</div>
<!-- モーダル・フッター -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">OK</button>
</div>
</form>
</div>
・・・
解決案 * JavaScriptのキーイベント・ハンドルでEnterキー押下時には動作を無効化する => ただし、textareaでは有効にするhttps://blogs.yahoo.co.jp/dk521123/28230461.html
<script>
$("#demoModal").on( 'keypress', function(event) {
if(event.keyCode === 13 && event.target.type != "textarea") {
// 無効化
console.log("[DEBUG] Enter key event cancel...");
return false;
}
});
</script>
参考文献 https://github.com/nikku/jquery-bootstrap-scripting/issues/34