我需要帮助,我发现这个tutorial用于验证表单而无需刷新,但问题是我认为这只适用于文本字段:)但我使用了4个不同的单选按钮,我真的需要这个脚本来处理这些单选按钮!
这是javascript文件:)!
runOnLoad(function(){
$("input#name").select().focus();
});
$(function() {
$('.error').hide();
$(".button").click(function() {
// validate and process form here
$('.error').hide();
var name = $("input#name").val();
if (name == "") {
$("label#name_error").show();
$("input#name").focus();
return false;
}
var email = $("input#email").val();
if (email == "") {
$("label#email_error").show();
$("input#email").focus();
return false;
}
var mobile = $("input#mobile").val();
if (mobile == "") {
$("label#mobile_error").show();
$("input#mobile").focus();
return false;
}
var college = $("input#college").val();
if (college == "") {
$("label#college_error").show();
$("input#college").focus();
return false;
}
var university = $("input#university").val();
if (university == "") {
$("label#university_error").show();
$("input#university").focus();
return false;
}
var level = $("input#level").val();
if (level == "") {
$("label#level_error").show();
$("input#level").focus();
return false;
}
var first_preference = $("input#first_preference").val();
if (first_preference == "") {
$("label#first_preference_error").show();
$("input#first_preference").focus();
return false;
}
var second_preference = $("input#second_preference").val();
if (second_preference == "") {
$("label#second_preference_error").show();
$("input#second_preference").focus();
return false;
}
var third_preference = $("input#third_preference").val();
if (third_preference == "") {
$("label#third_preference_error").show();
$("input#third_preference").focus();
return false;
}
var heard = $("input#heard").val();
if (heard == "") {
$("label#heard_error").show();
$("input#heard").focus();
return false;
}
var applying = $("input#applying").val();
if (applying == "") {
$("label#applying_error").show();
$("input#applying").focus();
return false;
}
var strength = $("input#strength").val();
if (strength == "") {
$("label#strength_error").show();
$("input#strength").focus();
return false;
}
var weakness = $("input#weakness").val();
if (weakness == "") {
$("label#weakness_error").show();
$("input#weakness").focus();
return false;
}
var previousEx = $("input#previousEx").val();
if (previousEx == "") {
$("label#previousEx_error").show();
$("input#previousEx").focus();
return false;
}
var dataString = 'name='+ name + '&email=' + email + '&mobile=' + mobile + '&college=' + college + '&university=' + university + '&level=' + level + '&first_preference=' + first_preference + '&second_preference=' + second_preference + '&third_preference=' + third_preference + '&heard=' + heard + '&applying=' + applying + '&strength=' + strength + '&weakness=' + weakness + '&previousEx=' + previousEx;
$.ajax({
type: "POST",
url: "php/database_sorting.php",
data: dataString,
success: function() {
$('#contact_form').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<img id='checkmark' src='images/done.png' />");
});
}
});
return false;
});
});发布于 2012-08-30 09:41:23
假设您有一个按名称分组的单选按钮,要检查是否选择了其中一个:
//update the name to correspond to your radios group's name
if (!$('input[type="radio"][name="radiosGroup"]:checked').length) {
alert('no radios selected!');
//you can adapt the error message to your liking,
//e.g. replacing the alert with $('#radio_error').show()
return false;
}另一种“跳过”单选按钮上的JS验证的方法是在生成页面时预选中的一个:
<input type="radio" name="radiosGroup" checked="checked" value="1" />这样,将始终选择其中一个无线电。
通过使用HTML5,您可以在input元素上使用required属性(按钮输入除外)来跳过现代浏览器的JS验证。
最后,如果所有的input/textarea/checkbox/radios都在表单中,那么可以使用jQuery的.serialize()生成查询字符串,而不是手动构建它。如果它们不在表单中,您可以将它们包装在一个表单中。
$('#myForm').serialize();正如问题中已经提到的,JS验证只是为了提供更好的UI (例如,在不刷新页面的情况下显示错误),如果您要公开您的站点,则需要服务器端验证,因为JS很容易被绕过。
另外,假设您的input位于表单内部,最好将验证附加到表单的.submit()处理程序,而不是单击按钮,以确保无论何时提交表单,无论是浏览器还是用户单击submit按钮或在文本输入元素中按Enter,都会触发验证。
https://stackoverflow.com/questions/12188564
复制相似问题