javascript - JavaScript 返回 false 但仍在提交
问题描述
这是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
按钮并且验证失败时,它仍然提交表单。
解决方案
您的validate
函数不返回任何内容。
这些return
语句属于传递给的匿名函数.each
。
推荐阅读
- javascript - 将不可变对象转换为普通对象
- react-native - 无法在我的视图中显示按钮
- c# - 了解 .Net 中的 32 位与 64 位
- react-native - 在 React Native 中更改应用程序背景颜色
- c# - .Net Core 2.1 - 无法访问已处置的对象。对象名称:'IServiceProvider'
- linux - 每隔两行剪切字段并将结果连接到没有空格的字符串
- jquery - 自动聚焦 div 内的下一个输入
- ruby-on-rails - 如何使用 Ruby 获取网站 (url) cookie 列表
- react-native - 反应原生 - 缺少srcipt:ios
- zendesk - Zendesk Chat 小部件状态检查