简体   繁体   English

提交表单时如何显示带有复选框的错误消息

[英]How to show error message with checkbox when I submit form

I want to show error message when I click the submit button. 单击提交按钮时,我想显示错误消息。 So suppose I have 3 checkboxes: (I show one because the other are the same). 因此,假设我有3个复选框:(我显示一个是因为另一个是相同的)。 Please add more code about the form. 请添加有关该表单的更多代码。 It is not debuggable at that level. 在该级别上不可调试。 I should have added this as comment, but you can see I don't have enough reputation score. 我应该添加此内容作为评论,但是您可以看到我的信誉评分不够。

<label>
     <input type="checkbox"  name="privacy" value="1" id="privacy" required="required" >
      <span class="text-gray" style="font-size: 12px;"> 
        <a href="/privacy-policy/" target="_blank">Privacy</a>
      </span>
</label>

All checkboxes have the attribute required="required" . 所有复选框都具有属性required="required" When I click on the submit button and the checkbox is not selected, I obtain two things: 当我单击“提交”按钮并且未选中该复选框时,我得到两个信息:
1) I don't read anything about the "checkbox is empty" that I must read for HTML5 default validation when the attribute is required 1)当需要该属性时,对于HTML5默认验证,我没有阅读任何有关“复选框为空”的信息
2) I obtain this error: An invalid form control with name='privacy' is not focusable. 2)我收到此错误: 名称='privacy'的无效窗体控件不可聚焦。

Anyone can help me? 有人可以帮助我吗?

 <script type="text/javascript">

  function checkForm(form)
  {

    if(!form.terms.checked) {
      alert("Please indicate that you accept the Terms and Conditions");
      form.terms.focus();
      return false;
    }
    return true;
  }

</script>

<form  onsubmit="return checkForm(this);">

<p><input type="checkbox" name="terms"> I accept the <u>Terms and Conditions</u></p>
<p><input type="submit"></p>
</form>

Use checked so that it will get checked and check the following code i guess it will help you 使用checked,以便将其检查并检查以下代码,我想它将对您有帮助

 $(document).ready(function() { $("#submit").click(function() { var chkinput = document.getElementById("privacy"); if (chkinput.checked) { alert("Checked!"); } else { alert ("Not checked"); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="checkbox" name="privacy" value="1" id="privacy" checked> <span class="text-gray" style="font-size: 12px;"> <a href="/privacy-policy/" target="_blank">Privacy</a><br/> <button id="submit">Submit</button> </span> </form> <form method="post"> <input type="checkbox" name="privacy" value="1" id="privacy1" required="required"> <span class="text-gray" style="font-size: 12px;"> <a href="/privacy-policy/" target="_blank">Privacy</a><br/> <button>Submit</button> </span> </form> 

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

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