1 回答
TA贡献1843条经验 获得超7个赞
您可以向窗体中的每个元素和模式触发器按钮添加事件侦听器,并循环遍历所有输入以显示模式(如果它们都具有非空值)。您还需要从模式触发器按钮中删除该属性,并且仅以编程方式打开模式。这将导致模式在用户单击离开他们填写的最后一个输入时打开。blur
input
data-target
const form = document.querySelector("#myForm");
const inputs = [...form.querySelectorAll("input")];
const modalTrigger = document.querySelector("button[type=submit]");
inputs.forEach(input=>{
input.addEventListener("blur", openModalIfAllFilled);
});
function openModalIfAllFilled(){
const allFilled = inputs.every(a=>a.value.trim());
if(allFilled){
$('#exampleModal').modal('show');
}
}
modalTrigger.addEventListener("click", openModalIfAllFilled);
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<form action="" id="myForm">
<fieldset>
<input type="hidden" name="action" value="contact_send" />
<div class="row">
<div class="col-md-4">
<label for="contact:name">Full Name *</label>
<input required type="text" value="" placeholder="Full name" class="form-control" name="fullname" id="contact:name">
</div>
<div class="col-md-4">
<label for="contact:compagny">Compagny Name *</label>
<input required type="text" value="" placeholder="Compagny Name" class="form-control" name="compagny" id="contact:compagny">
</div>
<div class="col-md-4">
<label for="contact:email">Email</label>
<input required type="email" value="" placeholder="email@email.com" class="form-control" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" id="contact:email">
</div>
</div>
<div class="row">
<div class="col-md-6">
<label for="contact:phone">Phone *</label>
<input type="tel" value="" class="form-control input-phone" placeholder="999 999-9999" name="phone" id="contact:phone" pattern="[0-9]{3} [0-9]{3}-[0-9]{4}" maxlength="12" onkeydown="return event.keyCode !== 69">
</div>
</div>
</fieldset>
</form>
<!-- Button trigger modal -->
<div class="row">
<div class="col-md-12">
<button type="submit" class="btn btn-primary active"><i class="fa fa-check"></i> SEND MESSAGE</button>
</div>
</div>
<!-- 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">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
这也可以用jQuery编写,以获得更好的浏览器支持。
$("#myForm input").blur(openModalIfAllFilled);
$("button[type=submit]").click(openModalIfAllFilled);
function openModalIfAllFilled(){
var notAllFilled = $('#myForm input').is(function(index, element){
return !element.value.trim();
});
if(!notAllFilled){
$('#exampleModal').modal('show');
}
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<form action="" id="myForm">
<fieldset>
<input type="hidden" name="action" value="contact_send" />
<div class="row">
<div class="col-md-4">
<label for="contact:name">Full Name *</label>
<input required type="text" value="" placeholder="Full name" class="form-control" name="fullname" id="contact:name">
</div>
<div class="col-md-4">
<label for="contact:compagny">Compagny Name *</label>
<input required type="text" value="" placeholder="Compagny Name" class="form-control" name="compagny" id="contact:compagny">
</div>
<div class="col-md-4">
<label for="contact:email">Email</label>
<input required type="email" value="" placeholder="email@email.com" class="form-control" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" id="contact:email">
</div>
</div>
<div class="row">
<div class="col-md-6">
<label for="contact:phone">Phone *</label>
<input type="tel" value="" class="form-control input-phone" placeholder="999 999-9999" name="phone" id="contact:phone" pattern="[0-9]{3} [0-9]{3}-[0-9]{4}" maxlength="12" onkeydown="return event.keyCode !== 69">
</div>
</div>
</fieldset>
</form>
<!-- Button trigger modal -->
<div class="row">
<div class="col-md-12">
<button type="submit" class="btn btn-primary active"><i class="fa fa-check"></i> SEND MESSAGE</button>
</div>
</div>
<!-- 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">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
添加回答
举报