我正在尝试验证两个字段。一个是开放时间,另一个是关闭时间。我的验证应该检查关闭时间是否具有除“00”以外的值,我的开放时间也应该具有大于“00”的值。
我的问题是,我做错了什么?我知道我的功能必须有错误。
这是我到目前为止所写的验证:
<script type="text/javascript">
function hoursFunction(formObject, formField, fieldValue)
{
var closeHours = document.getElementById('closeHours#CountVar#');
var openHours = document.getElementById('openHours#CountVar#');
if(closeHours != "00" && openHours == "00")
{
sfm_show_error_msg('Enter a valid opening time');
}
}
这是我的表格样本
<form id="hoursForm" action="physician_businessHours.cfm?docID=<cfoutput>#docid#</cfoutput>" method="post" onsubmit="hoursFunction();">
<input type="hidden" name="postitnow" value="yes">
<table border="1">
<tr>
<th>Day</th><th>Open Time</th><th>Close Time</th>
</tr>
<cfloop from="1" to="7" index="CountVar">
<cfset dayFound= 0>
<tr>
<td><cfif CountVar eq 1>Mon<cfelseif CountVar eq 2>Tues<cfelseif CountVar eq 3>Wednes<cfelseif CountVar eq 4>Thurs<cfelseif CountVar eq 5>Fri<cfelseif CountVar eq 6>Satur<cfelseif CountVar eq 7>Sun</cfif>day</td>
<cfoutput>
<td>
<select id="openHours#CountVar#" name="openHours#CountVar#">
<cfloop query="doctorHours">
<cfloop from="00" to="23" index="OpenHours">
<option value="#openHours#"<cfif TimeFormat(doctorHours.openTime,'HH') EQ OpenHours AND CountVar EQ doctorHours.day > selected="selected"</cfif>>#OpenHours#</option>
</cfloop>
</cfloop>
</select>
</td>
<td>
<select id="closeHours#CountVar#" name="closeHours#CountVar#">
<cfloop query="doctorHours">
<cfloop from="00" to="23" index="closeHours">
<option value="#closeHours#"
<cfif TimeFormat(doctorHours.closeTime,'HH') EQ closeHours AND CountVar EQ doctorHours.day > selected="selected"</cfif>>#closeHours#</option>
</cfloop>
</cfloop>
</select>
</td>
</tr>
<input type="hidden" name="Validate" onValidate="hoursFunction" message="You must select an opening hour">
<input type="submit" value="Update" id="Submit">
'
答案 0 :(得分:1)
您需要从元素中获取值:
var closeHours = document.getElementById('closeHours#CountVar#').value;
var openHours = document.getElementById('openHours#CountVar#').value;
答案 1 :(得分:1)
我明白了。您需要获取正确的元素ID:
for (var i = 0; i < 7; i++) {
var closeHours = document.getElementById('closeHours' + i).value;
var openHours = document.getElementById('openHours' + i).value;
if (closeHours != "00" && openHours == "00") {
sfm_show_error_msg('Enter a valid opening time');
}
}
答案 2 :(得分:1)
你的CountVar从1开始,而不是0,所以你的for循环应该是相同的。 另外,为防止表单提交,如果表单未验证,则hoursFunction()应返回false。 另外,你的hoursFunction()需要3个参数,但你没有传递任何参数 - 在这种情况下这些参数不是必需的,所以只需从函数定义中删除它们。 尝试以下功能:
function hoursFunction() {
for (var i = 1; i <= 7; i++) {
var closeHours = document.getElementById('closeHours' + i).value;
var openHours = document.getElementById('openHours' + i).value;
if (closeHours != "00" && openHours == "00") {
sfm_show_error_msg('Enter a valid opening time');
return false;
}
return true;
}
}
答案 3 :(得分:0)
这是我编写的代码,到目前为止工作正常:
function hoursFunction()
{
var i = 0;
var openHour;
var closeHour;
for(i=1;i<8;i++)
{
openHour = document.getElementById("openHours" + i).value;
closeHour= document.getElementById("closeHours" + i).value;
if(openHour > closeHour)
{
document.getElementById('error').innerHTML= "Error at " + i;
return false;
}
document.getElementById('error').innerHTML= "No Error occured";
return true;
}
}
我现在的问题是,当我提交表单时,表单似乎正在刷新并删除我的所有数据...... 有什么想法?