【问题标题】:I want my button to call my modal once the fields filled我希望我的按钮在填写完字段后调用我的模式
【发布时间】:2020-06-11 19:52:23
【问题描述】:

我希望得到一些帮助,以找出仅在填写这些字段后才能更轻松地召唤我的模态:

<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>
                            </form>

这是我的按钮:

<!-- Button trigger modal -->
                                <div class="row">
                                    <div class="col-md-12">
                                        <button type="submit" class="btn btn-primary active" data-toggle="modal" data-target="#exampleModal"><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">&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>
                                        </div>
                                    </div>
                                    </div>
                                     </div>

到目前为止,我的按钮触发了未填充字段的模式,我知道我需要一些 javascript 代码,作为一个没有任何经验的初学者,我很挣扎。

【问题讨论】:

    标签: javascript html bootstrap-4


    【解决方案1】:

    您可以将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">&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>
                                            </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">&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>
                                            </div>
                                        </div>
                                        </div>
                                         </div>

    【讨论】:

    • 感谢您的回答。我一定是做错了什么,因为......模态仍然出现......
    • @OlivierGodbout 你是什么意思?我的答案中的 sn-p 不起作用吗?
    • @OlivierGodbout 你能解释一下它的行为的哪一部分是不正确的吗?它对我来说很好。
    • 例如当我运行 sn-p 时,我不填写任何字段,按发送消息并显示模式!
    • @OlivierGodbout 我已经编辑了我的答案。看看它现在是否适合你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 2021-05-30
    • 1970-01-01
    • 2022-11-12
    • 2016-03-27
    • 2017-04-28
    • 2019-06-13
    相关资源
    最近更新 更多