首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我无法在提交表单之前选中复选框组

我无法在提交表单之前选中复选框组
EN

Stack Overflow用户
提问于 2019-03-01 04:09:29
回答 1查看 36关注 0票数 0

在提交表单之前,我需要验证表单上的多个checkboxGroups,但我不能这样做。

下面是我的代码:

代码语言:javascript
复制
<div class="form-wrapper">
                <form class="form1" action="update-2.php" id="profile" onClick="validate()" method="post">
                    <fieldset class="preguntas">

\-在这里我创建了7个具有不同id的复选框组,我将其中一个作为示例-\

代码语言:javascript
复制
<p>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Rimel" id="maquillaje">
                                Rimel</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Sombra" id="maquillaje">
                                Sombra</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Delineador" id="maquillaje">
                                Delineador</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Cuidado para sombracelhas" id="maquillaje">
                                Cuidado para sombracelhas</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Batom" id="maquillaje">
                                Batom</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Batom liquido" id="maquillaje">
                                Batom liquido</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Gloss" id="maquillaje">
                                Gloss</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Batom que aumenta labios" id="maquillaje">
                                Batom que aumenta làbios</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Delineador de labios" id="maquillaje">
                                Delineador de làbios</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Primer" id="maquillaje">
                                Primer</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Corretivo" id="maquillaje">
                                Corretivo</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Base hidratante" id="maquillaje">
                                Base hidratante</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Po corretivo" id="maquillaje">
                                Pó corretivo</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Blush" id="maquillaje">
                                Blush</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Iluminador" id="maquillaje">
                                Iluminador</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Bronzer e contorno" id="maquillaje">
                                Bronzer e contorno</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Pincéis e outros aplicadores de maquiagem" id="maquillaje">
                                Pincéis e outros aplicadores de maquiagem</label>
                              <br>

                          </p>

 <p>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Rimel" id="maquillaje">
                                Rimel</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Sombra" id="maquillaje">
                                Sombra</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Delineador" id="maquillaje">
                                Delineador</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Cuidado para sombracelhas" id="maquillaje">
                                Cuidado para sombracelhas</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Batom" id="maquillaje">
                                Batom</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Batom liquido" id="maquillaje">
                                Batom liquido</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Gloss" id="maquillaje">
                                Gloss</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Batom que aumenta labios" id="maquillaje">
                                Batom que aumenta làbios</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Delineador de labios" id="maquillaje">
                                Delineador de làbios</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Primer" id="maquillaje">
                                Primer</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Corretivo" id="maquillaje">
                                Corretivo</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Base hidratante" id="maquillaje">
                                Base hidratante</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Po corretivo" id="maquillaje">
                                Pó corretivo</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Blush" id="maquillaje">
                                Blush</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Iluminador" id="maquillaje">
                                Iluminador</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Bronzer e contorno" id="maquillaje">
                                Bronzer e contorno</label>
                              <br>
                              <label>
                                <input type="checkbox" name="maquillaje[]" class="option" value="Pincéis e outros aplicadores de maquiagem" id="maquillaje">
                                Pincéis e outros aplicadores de maquiagem</label>
                              <br>

                          </p>

我尝试用一个php错误处理程序来验证它:

代码语言:javascript
复制
if(empty($_POST['maquillaje'])){
header("location: wahtever.php?check");
}else{ .... all the code to write on the database...}

但它无论如何都会刷新页面,

我还尝试使用javascript来验证它。

代码语言:javascript
复制
<script>
    document.getElementById("profile").onsubmit = function () {
    var send = document.getElementById("maquillaje"),
        sendValue = send.value,
        sendCheck = send.checked,
        errors = "";
    if (!sendCheck) {
        errors += "Please answer question 5";
    }
if (errors != "") {
        alert(errors);
        return false;
}
        alert("Your details are being sent"); 
    return true; 
}

</script>

但我得到了相同的结果,它不会验证复选框,而是会刷新页面,即使复选框被选中...

我也尝试过Jquery,但我会一直收到警告文本框,而且它永远不会提交表单

我是一个新手,可能我犯了一个愚蠢的错误,请帮帮我!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-01 04:22:07

jQuery方法可能是您最简单的解决方案。

您需要添加的主要内容是"preventDefault“函数。这将阻止表单提交到后端。然后,您可以检查空白字段。如果没有错误,则通过添加this.submit()提交表单

代码语言:javascript
复制
<script>
$("#profile").submit(function(e) { // add 'e'
    e.preventDefault(); //add this line
    var send = document.getElementById("maquillaje"),
        sendValue = send.value,
        sendCheck = send.checked,
        errors = "";
    if (!sendCheck) {
        errors += "Please answer question 5";
    }
    if (errors != "") {
        alert(errors);
        return false;
    } else {
        alert("Your details are being sent"); 
        this.submit();  //submit the form  
        return true; 
    }
});
</script>

在文档中:https://api.jquery.com/event.preventdefault/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54933497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档