Javascript 如果选中复选框,则提交表单复选框
我使用带有“使用条款”复选框的表单进行注册Javascript 如果选中复选框,则提交表单复选框,javascript,jquery,html,Javascript,Jquery,Html,我使用带有“使用条款”复选框的表单进行注册 <form action="/signup/signup_success.html" id="regform" class="form-horizontal" novalidate="novalidate" autocomplete="off"> <fieldset> <div class="checkbox"> <label for="agb" id=
<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>
请接受使用条款
对
登记
//检查是否选中了“使用条款”复选框
$(“#regform”).submit(函数(事件){
if($('agbcheckbox').prop('checked')){
$(“#复选框_验证”).removeClass('show').addClass('hide');
//选中使用条款复选框
}否则{
//未选中“使用条款”复选框
$(“#复选框_验证”).removeClass('hide').addClass('show');
}
});
如果我将“action=“/signup/signup\u success.html”添加到我的表单中,复选框验证将不再有效。如果我将action设置为action=“”,验证将正常工作。我没有发现问题。感谢您的帮助!这就是您需要的
event.preventDefault();
还建议阅读此内容以更好地理解事件.preventDefault
和return false
发现问题-我的函数没有返回false;无论操作=“…”是否存在,验证仍然有效。区别在于,当定义了操作=“…”时,浏览器将按照指示发布表单。为防止在验证失败时发生这种情况,请调用e.preventDefault()
(例如)
$(“#regform”).submit(函数(e){
如果(…验证失败…){
…请在此处输入您的错误消息
e、 preventDefault()//防止浏览器执行默认操作(在本例中,提交表单)
}
});
这是更好的选择!谢谢Ergec