复选框一直显示错误消息

时间:2018-01-07 08:27:15

标签: javascript php jquery

即使选中复选框,我也会在提交表单时收到javascript警告错误消息?

这是我表格的一部分

<form onsubmit="return false" action="" method="POST" autocomplete="off" id="register_form">

<div class="form-check">
 <label for="field_terms" class="form-check-label">
   <input class="form-check-input" id="field_terms" type="checkbox" name="terms" value="check" required> By clicking on "<span style="color: orange">Create a free account</span>", I agree to the ...  <a href="terms_of_service.php"> Terms of Service </a> and <a href="privacy_policy.php"> Privacy Policy</a>.
  </label>
</div>

  <input type="submit" class="btn btn-block btn-primary" name="submit" role="button" onclick="send()" value="Create account" />

</form>

在我的表单中找到的javascript

<script>
function send(){
var term = document.getElementById("field_terms").value;

if(!term.checked) {
   alert("Please indicate that you accept the Terms and Conditions");

  } else {

        var parameters="field_terms="+term;

        var ajax=new XMLHttpRequest();

        ajax.onreadystatechange=function(){
            if(ajax.readyState === 4 && ajax.status === 200) {
                alert(ajax.responseText);


    }

};

        ajax.open("POST","register.php",false);
        ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        ajax.send(parameters);
    }
}

<script>

这是我的register.php

if(!empty($_POST) && isset($_POST['submit'])){
 ...

if($_POST['field_terms'] != "check"){
    $errors[''] = "Term & condition is not check";
    exit();

 //Note: I'm not inserting this value into my db, I just need to verify is whether user check this box or not to proceed further

} else {
...
}
}

为什么当我点击提交按钮时,我会一直收到javascript错误消息。

1 个答案:

答案 0 :(得分:1)

import java.util.logging.Level import java.util.logging.Logger Logger.getLogger("javax.jmdns").setLevel(Level.OFF) 语句始终为if(term.checked),因为false变量不是term实际上是此类元素的值。你应该使用:

HTMLInputElement

而不是:

var term = document.getElementById("field_terms");