这是 HTML:
<form method="post" action="http://localhost:8080/center/add" onsubmit="return validate()">
<div class="col-lg-12">
<table id="myTable">
<thead>
<tr>
<td>
Start Time
</td>
<td>
End Time
</td>
<td>
</td>
</tr>
</thead>
<tbody>
<tr class="t-row" title="" style="background: rgb(255, 255, 255);">
<td>
<input type="text" onfocus="clearError(this)" class="form-control time-box-width m-t-10px vTimeStart" name="vTimeStart[]" placeholder="Please enter start time in 24 hrs format">
</td>
<td>
<input type="text" onfocus="clearError(this)" class="form-control time-box-width m-t-10px vTimeEnd" name="vTimeEnd[]" placeholder="Please enter end time in 24 hrs format">
</td>
<td>
<input type="button" class="days-btn m-t-10px" value="Delete">
</td>
</tr>
<tr class="t-row" title="" style="background: rgb(255, 255, 255);">
<td><input type="text" name="vTimeStart[]" placeholder="Please enter start time in 24 hrs format" onfocus="clearError(this)" class="vTimeStart form-control time-box-width m-t-10px"></td>
<td><input onfocus="clearError(this)" type="text" name="vTimeEnd[]" placeholder="Please enter end time in 24 hrs format" value="" class="vTimeEnd form-control time-box-width m-t-10px"></td>
<td><input type="button" value="Delete" class="days-btn m-t-10px"></td>
</tr>
</tbody>
</table>
<div class="divide-div"></div>
</div>
<div class="modal-footer m-footer" style="border-top: 0;">
<input class=" days-btn m-t-10px" id="submit" type="submit" value="Submit">
<input class=" days-btn m-t-10px" id="close-popup" data-dismiss="modal" type="button" value="Close">
</div>
</form>
JavaScript:
function validate() {
$('.t-row').each(function(i, obj) {
var currentStartTimeValue = $('#myTable .vTimeStart').eq(i).val();
var currentEndTimeValue = $('#myTable .vTimeEnd').eq(i).val();
if(currentStartTimeValue != '') {
var v = valdateFormat(currentStartTimeValue);
alert(v);
if(!v) {
alert('Please enter value in HH:MM format.');
return false;
}
}
if(currentEndTimeValue != '') {
var v = valdateFormat(currentEndTimeValue);
if(!v) {
alert('Please enter value in HH:MM format.');
return false;
}
}
if(i > 0){
var previousIndex = i - 1;
var lastEndTimeValue = $('#myTable .vTimeEnd').eq(previousIndex).val();
alert(currentStartTimeValue);
var v = valdateFormat(currentStartTimeValue);
if(currentStartTimeValue < lastEndTimeValue){
$(this).attr('title','Current StartTime must be lesser than current EndTime!');
alert('Current StartTime cannot be lesser than previous EndTime');
return false;
}
}
if(!currentStartTimeValue){
var v = valdateFormat(currentStartTimeValue);
if(!v) {
alert('Please enter value in HH:MM format.');
return false;
}
$(this).attr('title','Enter value for Start Time!');
alert('Enter value for Start Time!');
return false;
} else if(!currentEndTimeValue){
var v = valdateFormat(currentEndTimeValue);
if(!v) {
alert('Please enter value in HH:MM format.');
return false;
}
$(this).attr('title','Enter value for End Time!');
alert('Enter value for End Time!');
return false;
} else if(currentStartTimeValue >= currentEndTimeValue){
$(this).attr('title','Current StartTime must be lesser than current EndTime!');
alert('Current StartTime must be lesser than current EndTime');
return false;
}
if(i === $('.t-row').length - 1) {
// alert('All good!');
}
});
// return true;
}
我的表单正在动态添加tr
,有一个按钮add row to bottom
。当我单击Submit
按钮并且验证失败时,仍然会提交表单。
答案 0 :(得分:1)
您的validate
函数不返回任何内容。
return
语句属于传递给.each
的匿名函数。
答案 1 :(得分:1)
您的return false
当前位于each
内-返回each
内不会导致父函数也返回false
。
在函数内部设置一个布尔值,然后在each
结束后返回它,例如:
function validate() {
let valid = true; // <-----------------
$('.t-row').each(function(i, obj) {
var currentStartTimeValue = $('#myTable .vTimeStart').eq(i).val();
var currentEndTimeValue = $('#myTable .vTimeEnd').eq(i).val();
if(currentStartTimeValue != '') {
var v = valdateFormat(currentStartTimeValue);
alert(v);
if(!v) {
alert('Please enter value in HH:MM format.');
valid = false; // <-----------------
return false;
}
}
if(currentEndTimeValue != '') {
var v = valdateFormat(currentEndTimeValue);
if(!v) {
alert('Please enter value in HH:MM format.');
valid = false; // <-----------------
return false;
}
}
// ...
});
return valid; // <-----------------
}
一旦发现问题,您仍然需要在return false
内each
才能立即终止循环。
答案 2 :(得分:1)
您将在每个循环中返回值,而这些循环又将不允许您的“ validate()”函数返回值。您可以在函数中定义一个局部布尔变量,并根据您在循环内的条件更改其值,然后在循环外返回它。
请参见下面的代码
.visually-hidden {
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
clip: rect(1px 1px 1px 1px); /* IE6, IE7 */ <- if you REALLY must support ie7 and 6!
clip: rect(1px, 1px, 1px, 1px);
white-space: nowrap;
}