admin管理员组文章数量:1277406
I'm using some Ajax / Javascript to prevent the form submit if an input field is empty.
$(function() {
var form = $('#contact-form-edc');
$(form).submit(function(e) {
e.preventDefault();
var formData = $(form).serialize();
if (document.getElementById("nachricht-field").value.length < 10) {
document.getElementById('error-nachricht').style.display = "";
} else {
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
})
.done(function(response) {
document.getElementById('success').style.display = "";
document.getElementById('error').style.display = "none";
document.getElementById('error-nachricht').style.display = "none";
$('input').val('');
$('textarea').val('');
$('.button').val('Abschicken');
$('input[name=datenschutz]').attr('checked', false);
})
.fail(function(data) {
document.getElementById('error').style.display = "";
document.getElementById('success').style.display ="none";
});
}
});
});
The script works fine but do not note the checkbox of the form. The checkbox
<input type="checkbox" name="datenschutz" value="Datenschutz" required="required" >
is required as well but the user can submit the form without checking the checkbox. Is there a small script which I can implement in the current Ajax-Script?
I'm using some Ajax / Javascript to prevent the form submit if an input field is empty.
$(function() {
var form = $('#contact-form-edc');
$(form).submit(function(e) {
e.preventDefault();
var formData = $(form).serialize();
if (document.getElementById("nachricht-field").value.length < 10) {
document.getElementById('error-nachricht').style.display = "";
} else {
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
})
.done(function(response) {
document.getElementById('success').style.display = "";
document.getElementById('error').style.display = "none";
document.getElementById('error-nachricht').style.display = "none";
$('input').val('');
$('textarea').val('');
$('.button').val('Abschicken');
$('input[name=datenschutz]').attr('checked', false);
})
.fail(function(data) {
document.getElementById('error').style.display = "";
document.getElementById('success').style.display ="none";
});
}
});
});
The script works fine but do not note the checkbox of the form. The checkbox
<input type="checkbox" name="datenschutz" value="Datenschutz" required="required" >
is required as well but the user can submit the form without checking the checkbox. Is there a small script which I can implement in the current Ajax-Script?
Share Improve this question edited Dec 9, 2015 at 12:11 Legionar 7,6073 gold badges44 silver badges71 bronze badges asked Dec 9, 2015 at 12:02 susanloeksusanloek 4111 gold badge5 silver badges20 bronze badges 1- varChecked = $('input[name="datenschutz"]:checked').length; if(Checked>0){ //do stuff here }else{ //do stuff here } – Rahul Saxena Commented Dec 9, 2015 at 12:08
2 Answers
Reset to default 9Use this way:
$(form).submit(function(e) {
e.preventDefault();
if ($(this).find('input[name="datenschutz"]')[0].checked === false)
return false;
// rest of form
If you want to add a message, you can go ahead this way:
$(form).submit(function(e) {
e.preventDefault();
if ($(this).find('input[name="datenschutz"]')[0].checked === false) {
alert("Please accept the terms and conditions!");
return false;
}
// rest of form
I'd suggest enabling the submit button when the check-box is checked:
// binding an anonymous function to handle the change event:
$('input[name=datenschutz]').on('change', function () {
var input = this;
// navigating to the form 'this.form',
// finding the submit button (obviously correct the
// selector),
// setting the disabled property of the submit button to
// to be false when the check-box is checked, and true
// when the check-box is unchecked:
$(input.form).find('submitButtonSelector').prop('disabled', !input.checked});
});
本文标签: javascriptPrevent form submit if checkbox is uncheckedStack Overflow
版权声明:本文标题:javascript - Prevent form submit if checkbox is unchecked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741233383a2362524.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论