简体   繁体   English

HTML / JavaScript:提交表单(使用queryselector)时如何使用警报?

[英]HTML/JavaScript: How to use alert when submitting a form (with queryselector)?

I wrote some HTML form where the form should alert when nothing is submitted. 我写了一些HTML表单,该表单应在未提交任何内容时发出警报。 I started with creating this for the names. 我从创建名称开始。 As expected, the message 'You must provide your full name!' 如预期的那样,消息“您必须提供全名!” shows when no name is entered. 当未输入名称时显示。 However, the message also shows up when the names are actually entered… 但是,当实际输入名称时,也会显示该消息…

{% extends "layout.html" %} {%扩展了“ layout.html”%}

{% block main %} {%block main%}

<!-- http://getbootstrap.com/docs/4.1/content/typography/ -->
<h1 class="mb-3">Form</h1>

<!-- http://getbootstrap.com/docs/4.1/components/forms/ -->
<form action="/form" method="post" id="form_id">

    <div class="form-row names">
        <div class="form-group col-md-3 name1">
            <label for="inputFirstname">First name</label>
            <input type="name" autofocus class="form-control" id="inputFirstname" placeholder="First name">
        </div>
        <div class="form-group col-md-3 name2">
            <label for="inputLastname">Last name</label>
            <input type="name" class="form-control" id="inputLastname" placeholder="Last name">
        </div>
    </div>
    <div class="form-row clubs">
        <div class="form-group col-md-3">
            <label cfor="inlineFormCustomSelect">Club preference</label>
            <select class="custom-select" id="inlineFormCustomSelect">
                <option disabled selected value> -- select an option -- </option>
                <option value="1">PSV</option>
                <option value="2">Ajax</option>
                <option value="3">Feyenoord</option>
                <option value="4">De Graafschap</option>
                <option value="5">RKVV Bergeijk 2</option>
            </select>
            <div class="invalid-feedback">Please choose one option</div>
        </div>
    </div>
    <div class="form-group">
        <label for="PFoot">Preferred foot</label>
        <label class="form-check-label">
            <input type="radio" id="PFoot" name="algorithm" value="right">
            Right
        </label>
        <label class="form-check-label">
            <input type="radio" id="PFoot" name="algorithm" value="left">
            Left
        </label>
        <label class="form-check-label">
            <input type="radio" id="PFoot" name="algorithm" value="zweipotig">
            Zweipotig
        </label>
    </div>

    <!-- http://getbootstrap.com/docs/4.1/components/buttons/ -->
    <button class="btn btn-primary" type="submit">Submit</button>

</form>

<script>
    document.querySelector('form').onsubmit = function() {
        if (!document.querySelector('.name1').value) {
            alert('You must provide your full name!');
            return false;
        }

        return true;
    }

</script>

{% endblock %} {%endblock%}

As expected, the message 'You must provide your full name!' 如预期的那样,消息“您必须提供全名!” shows when no name is entered. 当未输入名称时显示。 However, the message also shows up when the names are actually entered… 但是,当实际输入名称时,也会显示该消息…

You are using the wrong selector ( .name1 ) which refers a div element, not the input you are thinking. 您使用错误的选择器( .name1 )引用了div元素,而不是您在考虑的输入。 Try #inputFirstname 尝试#inputFirstname

 document.querySelector('form').onsubmit = function() { if (!document.querySelector('#inputFirstname').value) { alert('You must provide your full name!'); return false; } else if(!document.querySelector('[name=algorithm]:checked')){ alert('You must provide Preferred foot!'); return false; } return true; } 
 <form action="/form" method="post" id="form_id"> <div class="form-row names"> <div class="form-group col-md-3 name1"> <label for="inputFirstname">First name</label> <input type="name" autofocus class="form-control" id="inputFirstname" placeholder="First name"> </div> <div class="form-group col-md-3 name2"> <label for="inputLastname">Last name</label> <input type="name" class="form-control" id="inputLastname" placeholder="Last name"> </div> </div> <div class="form-row clubs"> <div class="form-group col-md-3"> <label cfor="inlineFormCustomSelect">Club preference</label> <select class="custom-select" id="inlineFormCustomSelect"> <option disabled selected value> -- select an option -- </option> <option value="1">PSV</option> <option value="2">Ajax</option> <option value="3">Feyenoord</option> <option value="4">De Graafschap</option> <option value="5">RKVV Bergeijk 2</option> </select> <div class="invalid-feedback">Please choose one option</div> </div> </div> <div class="form-group"> <label for="PFoot">Preferred foot</label> <label class="form-check-label"> <input type="radio" id="PFoot" name="algorithm" value="right"> Right </label> <label class="form-check-label"> <input type="radio" id="PFoot" name="algorithm" value="left"> Left </label> <label class="form-check-label"> <input type="radio" id="PFoot" name="algorithm" value="zweipotig"> Zweipotig </label> </div> <!-- http://getbootstrap.com/docs/4.1/components/buttons/ --> <button class="btn btn-primary" type="submit">Submit</button> </form> 

The class name1 is div, not an input. name1是div,不是输入。 Then you have to use (".name1 input") in the querySelector 然后,您必须在querySelector中使用(“ .name1输入”)

document.querySelector('form').onsubmit = function() {
    if (!document.querySelector('.name1 input').value) {
        alert('You must provide your full name!');
        return false;
    }

    return true;
}

in my case it's worked 以我为例

<script>
document.querySelector('form').onsubmit = function() {
    if (!document.querySelector('#inputFirstname').value) {
        alert('You must provide your full name!');
        return false;
    }

    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM