[英]submit form check if checkbox is checked
I use a form for signup with a checkbox with 'terms of use' 我使用带有“使用条款”复选框的注册表单
<form action="/signup/signup_success.html" id="regform" class="form-horizontal" novalidate="novalidate" autocomplete="off">
<fieldset>
<div class="checkbox">
<label for="agb" id="checkbox_validate" class="hide success">Please accept Terms of Use</label>
<label>
<input type="checkbox" name="agbcheckbox" id="agbcheckbox">
Yes <a target="_blank" href="http://example.de">Datenschutzbestimmungen</a>. </label>
</div>
<div class="buttonWrapper">
<button type="submit" class="try-button" >
Register
</button>
</div>
</fieldset>
</form>
<script>
//check if checkbox for terms of use is checked or not
$("#regform").submit(function(event) {
if ($('#agbcheckbox').prop('checked')) {
$('#checkbox_validate').removeClass('show').addClass('hide');
//checkbox for terms of use is checked
} else {
//checkbox for terms of use is NOT checked
$('#checkbox_validate').removeClass('hide').addClass('show');
}
});
</script>
if I add "action="/signup/signup_success.html" to my form, the checkbox validation do not work anymore. If I set the action to action="" the validation works correctly. I don´t find the problem. Thank you for your help! 如果我在表单中添加“ action =“ / signup / signup_success.html”,则复选框验证将不再起作用。如果将action设置为action =”“,验证将正常工作。找不到问题。谢谢您的帮助!
This is what you need 这就是你所需要的
event.preventDefault();
http://jsfiddle.net/ergec/L8Y8s/ http://jsfiddle.net/ergec/L8Y8s/
Also suggest to read this to better understand the difference between event.preventDefault
and return false
还建议阅读此内容以更好地了解
event.preventDefault
和return false
之间的区别
event.preventDefault() vs. return false event.preventDefault()与返回false
发现了问题-我的函数错过了返回false的机会;
Your validate still works regardless of action='...' exists or not. 无论action ='...'是否存在,您的验证仍然有效。 The difference is that when you have action='...' defined, the browser will post your form as directed.
区别在于,当您定义action ='...'时,浏览器将按照指示发布您的表单。 To prevent this happening in the event of validation failure, you call e.preventDefault()
为防止验证失败时发生这种情况,请调用e.preventDefault()
eg) $("#regform").submit(function(e){ if (.. validation failes ...) { .. do your error msg here e.preventDefault() // prevent browser to perform default action (in this case submit the form) } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.