I have a form that I want jquery to check and see if the input/select values with a class of required (These classes are in the #customerForm form) are blank. If they are I would like to prevent the form from submitting and change the color of the input/select box to red. If all of the input/select values with a class of required do have values, I would like to submit the form, fade the form inputs out and and fadeIn the next form. Here's my code:
我有一个表单,我希望jquery检查并查看具有所需类(这些类是#customerForm形式)的输入/选择值是否为空。如果他们是我想阻止表单提交并将输入/选择框的颜色更改为红色。如果具有所需类的所有输入/选择值都具有值,我想提交表单,淡出表单输入并淡入下一个表单。这是我的代码:
$('#customerForm').submit(function (event) {
var value = $('.required').each();
if (value === "") {
value.addClass("invalid");
return false;
} else {
value.removeClass("invalid");
$('#billForm, #shipForm').fadeOut();
$('#payment-form').fadeIn();
}
});
The problem is the form doesn't check for the input values and submits the form anyways. Any help would be amazing.
问题是表单不检查输入值并反正提交表单。任何帮助都会很棒。
3 个解决方案
#1
1
$('.required').each() returns an array of inputs.
$('。required')。each()返回一个输入数组。
You have to loop through each input and check the value before the form can be submit.
您必须遍历每个输入并检查值,然后才能提交表单。
$( ".required" ).each(function() {
//validation
});
more complete example:
更完整的例子:
$('#customerForm').submit(function (event) {
var error = false;
$( ".required" ).each(function() {
if ($(this).val() === "")
{
$(this).addClass("invalid");
error = true;
}
else
{
$(this).removeClass("invalid");
}
});
if(!error)
{
$('#billForm, #shipForm').fadeOut();
$('#payment-form').fadeIn();
}
else
{
return false;
}
});
#2
0
You cant grab a field using each. Each is to iterate through them.
你不能使用每个领域抓住一个领域。每个都是迭代它们。
So you need to do this.
所以你需要这样做。
$('#customerForm').submit(function (event) {
var value = $('.required'); // Get all the required fields
var cnt=0; // setup a counter
// Loop therough required fields and apply error class based on value
$.each(value, function(i,val){
if (val.val() === "") {
cnt++;//add to the count of invalid fields
val.addClass("invalid");// apply error class
} else {
val.removeClass("invalid");
}
});
// Check the cnt variable is larger than 0, means we have errors
if(cnt!=0){
return false;
// otherwise, continue
} else {
$('#billForm, #shipForm').fadeOut();
$('#payment-form').fadeIn();
}
});
#3
0
You should loop through each element like this
你应该像这样遍历每个元素
$('.required').each((function( index ) {
var error = 0;
value = $(this).html();
if (value === "") {
this.addClass("invalid");
error = 1;
} else {
this.removeClass("invalid");
$('#billForm, #shipForm').fadeOut();
$('#payment-form').fadeIn();
}
if (error)
return false;
});
#1
1
$('.required').each() returns an array of inputs.
$('。required')。each()返回一个输入数组。
You have to loop through each input and check the value before the form can be submit.
您必须遍历每个输入并检查值,然后才能提交表单。
$( ".required" ).each(function() {
//validation
});
more complete example:
更完整的例子:
$('#customerForm').submit(function (event) {
var error = false;
$( ".required" ).each(function() {
if ($(this).val() === "")
{
$(this).addClass("invalid");
error = true;
}
else
{
$(this).removeClass("invalid");
}
});
if(!error)
{
$('#billForm, #shipForm').fadeOut();
$('#payment-form').fadeIn();
}
else
{
return false;
}
});
#2
0
You cant grab a field using each. Each is to iterate through them.
你不能使用每个领域抓住一个领域。每个都是迭代它们。
So you need to do this.
所以你需要这样做。
$('#customerForm').submit(function (event) {
var value = $('.required'); // Get all the required fields
var cnt=0; // setup a counter
// Loop therough required fields and apply error class based on value
$.each(value, function(i,val){
if (val.val() === "") {
cnt++;//add to the count of invalid fields
val.addClass("invalid");// apply error class
} else {
val.removeClass("invalid");
}
});
// Check the cnt variable is larger than 0, means we have errors
if(cnt!=0){
return false;
// otherwise, continue
} else {
$('#billForm, #shipForm').fadeOut();
$('#payment-form').fadeIn();
}
});
#3
0
You should loop through each element like this
你应该像这样遍历每个元素
$('.required').each((function( index ) {
var error = 0;
value = $(this).html();
if (value === "") {
this.addClass("invalid");
error = 1;
} else {
this.removeClass("invalid");
$('#billForm, #shipForm').fadeOut();
$('#payment-form').fadeIn();
}
if (error)
return false;
});