我有多个输入字段,它们具有相同的类名。我需要检查以确保其中至少有1个不为空。所以我有
$('form[name=expForm]').submit(function(){
var msg = "";
var found = false;
$('.date-mask').each(function(){
if($(this).val()){
found = true;
}
});
if (found != true) {
msg + =“提交前请至少提供1个日期。\ n”;
返回false;
}
var calcFnd = false;
$('。calc')。each(function(){
if($(this).val()){
calcFnd = true;
}
});
如果(calcFnd!= true){
msg + =“提交前,请至少提供1笔费用。\ n”;
返回false;
}
if(msg!=“”){
警报(msg);
返回false;
}
if($('.ttlR27').val()==""){
var net = $('.ttlR26').val();
$('.ttlR28').val(net);
}
return false;
});
<cfloop from="1" to="#ArrayLen(labels)#" index="r">
<tr>
<td class="labels <cfif labels[r] EQ "Day of Week:">row1</cfif>"><cfif ArrayIsDefined(labels,r) AND labels[r] NEQ "Open1"><cfif labels[r] EQ "Open"><input type="text" id="descript#r#" name="descript#r#" class="description descript#r#" value="Enter text here" style="width:auto;" /><cfelse>#labels[r]#</cfif></cfif></td>
<cfloop from="1" to="7" index="i">
<td id="Day#i#" class="row#r# col#i#">
<cfif r EQ 1>#Left(DayOfWeekAsString(i),3)#<cfelse><cfif r EQ 2>
<input type="text" class="date-mask" name="dates#i#" required="yes" message="Please provide at least 1 date before submitting.">
<cfelse>
<input type="text"
<cfif labels[r] EQ "Personal Car: Mileage ##"> id="gasamount#i#" <cfelseif labels[r] EQ "Personal Car: Mileage $">id="gasmoney#i#"</cfif><cfif labels[r] EQ "Open">id="open#r#"</cfif><cfif labels[r] EQ "Daily Totals">id="dailytotals#i#"</cfif> class="all <cfif labels[r] EQ "Personal Car: Mileage ##">gasamount <cfelse><cfif labels[r] NEQ "Daily Totals">C#i# </cfif></cfif><cfif labels[r] EQ "Personal Car: Mileage $">gasmoney<cfelse>calc R#r#<cfif labels[r] EQ "Daily Totals"> </cfif></cfif><cfif labels[r] EQ "Daily Totals">ttlC#i#</cfif><cfif labels[r] EQ "Less Advance(if applicable)"> less</cfif><cfif labels[r] EQ "Net Due Employee"> net</cfif><cfif labels[r] EQ "Open"> open</cfif>"
<cfif labels[r] EQ "Daily Totals" OR labels[r] EQ "Personal Car: Mileage $" OR labels[r] EQ "Open1">readonly="readonly"</cfif>
name="<cfif labels[r] NEQ "Personal Car: Mileage ##" AND labels[r] NEQ "Personal Car: Mileage $" AND labels[r] NEQ "Dates:" AND labels[r] NEQ "Open1" AND labels[r] NEQ "Daily Totals">R#r#.C#i#</cfif><cfif labels[r] EQ "Personal Car: Mileage ##">gasamt#i#</cfif><cfif labels[r] EQ "Daily Totals">celltotals#i#</cfif><cfif labels[r] EQ "Personal Car: Mileage $">gastot#i#</cfif>" /></cfif>
</cfif>
</td>
</cfloop>
<td class="totals<cfif r EQ 1>1</cfif>"><cfif r EQ 1>Total<cfelse><input type="text" <cfif labels[r] EQ "Less Advance(if applicable)">id="less"</cfif><cfif labels[r] EQ "Net Due Employee">id="net"</cfif>id="totals" class="ttlR#r#" name="totals#r#" readonly="readonly" /></cfif></td>
</tr>
</cfloop>
正在使用的类名称为:date-mask(日期的第一行)和calc(表格的其余部分)。
我只需要1个输入就可以不留空,这样才能进行真正的提交。
有任何想法吗?
编辑
这是实时页面的链接。我在这里验证的是两件事,但本质上是相同的功能。第一行,日期。还有,整个桌子。如果不是全部,则几乎每个输入都具有相同的类之一。
最佳答案
@ karim79的代码应该起作用。在这里,我写了一个有效的概念证明:
剧本:
function checkFields() {
var found = false;
$('.huh').each(function(){
if($(this).val()){
found = true;
return false;
}
});
if (found == true) {
alert("at least one field has value");
} else {
alert("all fields are empty");
}
}
标记
<input type="text" class="huh" name="limit1" />
<input type="text" class="huh" name="limit2" />
<input type="text" class="huh" name="limit3" />
<input type="text" class="huh" name="limit4" />
<input type="text" class="huh" name="limit5" />
<input type="button" name="submit" value="Submit" onclick="checkFields();"/>
自己在干净的html上尝试此代码,您会看到它确实有效。
编辑添加的代码:该代码未到达警报部分,因为您已经在此处返回false:msg + =“请在提交之前至少提供1个日期。\ n”;返回false;在显示警报之前不要返回