私は親切にも、モーダルフォームの実装に関する助けが必要です。モーダルフォームのトリガをボタンからリンクに変更するにはどうすればよいですか?
以下のコードはW3Schoolsから適応されましたが、リンクするには(完全なコード体では以下のように)ボタンからモーダルトリガを変更する際に問題があります。
私は、ボタンのトリガーを交換しようとしている:
<a href="id01">Change Password</a>
しかし、それは動作しません。リンクをクリックすると、存在しないURLが要求されます。この問題を解決する方法について私に助言してもらえますか?
完全なコードは以下の通りです:
<div id='changePasswordModalButton'>
<!-- Button to open the modal Change Password form -->
<button onclick="document.getElementById('id01').style.display = 'block'">Change Password</button>
</div>
<!-- The Modal -->
<div id="id01" class="modal">
<span onclick="document.getElementById('id01').style.display = 'none'" class="close" title="Close Modal">×</span>
<!-- Modal Content -->
<form class="modal-content animate" action="/action_page.php">
<div class="container">
<label><b>Current Password*</b></label>
<input type="password" name="currentPassword" placeholder="Enter your current password" value="" required/>
<label><b>New Password*</b></label>
<input type="password" name="currentPassword" placeholder="Enter new password" value="" required/>
<label><b>Confirm New Password*</b></label>
<input type="password" name="currentPassword" placeholder="Confirm new password" value="" required/>
<button type="submit">Update Password</button>
<div class="container" style="background-color:#f1f1f1">
<button type="button" onclick="document.getElementById('id01').style.display = 'none'" class="cancelbtn">Cancel</button>
</div>
<div id="mandatoryFields">
<h4>* Mandatory Fields</h4>
</div>
</div>
</form>
</div>
<script>
// Get the modal
var modal = document.getElementById('id01');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
if (event.target === modal) {
modal.style.display = "none";
}
};
</script>
ありがとう、Derlin。 –
onclickの内側でonclickを処理することはお勧めしません。元々のようにイベントハンドラを使用し、 'Event.preventDefault()'関数を使用する方が良い方法です。古い方法は 'false'を返すことでしたが、これはもはやお勧めできません。参照:https://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-cancelation – spex
私は同意しますが、初心者の方は古いトリックを使用することをお勧めします(それを理解してから)jQueryやその他のツールに移ります。 – Derlin