我有以下功能:
function calcTotal(event)
{var myId = event.currentTarget.id;
myId = myId.replace(/[^0-9]/g, '');
var timeRegex = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
if($('#start'+myId).val().match(timeRegex) && $('#end'+myId).val().match(timeRegex) && $('#pause'+myId).val().match(timeRegex))
{ alert('win');
var minutes = 0;
var n = $('#end'+myId).val().split(':');
minutes = parseInt(n[0])*60 + parseInt(n[1]);
var n = $('#start'+myId).val().split(':');
minutes -= parseInt(n[0])*60 + parseInt(n[1]);
var n = $('#pause'+myId).val().split(':');
minutes -= parseInt(n[0])*60 + parseInt(n[1]);
var hours = Math.floor(minutes/60);
minutes = minutes % 60;
alert(hours + ':' + minutes);
$('#total' + myId).val(hours + ':' + minutes);
}
else
{ alert('fail');
$('#total' + myId).val('00:00');
}
}
</script>
让我们假设 myID 等于 1 并且让我们假设以下值:
start1 = 00:00
end1 = 10:00
pause1 = 02:00
我想让这个函数将前三个值与正则表达式匹配,以确定它们是否是有效时间。之后我想计算当天的总工作时间:end1-start1-pause1。出于某种原因,即使有这些正确的值,该函数也永远不会进入 if(....){ alert('win')...}。
我认为我的正则表达式有问题,但我已经检查过(stackoverflow question)并使用了那个正则表达式。
谁能告诉我我做错了什么?
整个代码:
<?php
if(isset($_POST['date']))
{ $sql = "INSERT INTO `hour_administration`.`hours` (`h_id`, `date`, `start`, `end`) VALUES";
foreach($_POST['date'] as $k => $v)
{
if(empty($_POST['date'][$k]))
break;
if($k > 0)
$sql .= ",";
$sql .= "(NULL, '".$_POST['date'][$k]."', '".$_POST['start_time'][$k].":00', '".$_POST ['end_time'][$k].":00')";
}
$sql .= ";";
echo " Query: ".$sql. "</BR>";
mysql_query($sql) or die('Could not query: ' . mysql_error());
}
if(isset($_POST['rows']))
{
$rows = $_POST['rows'];
}else
{
$rows = 5;
}
?>
<script>
$(document).ready(function() {
$( ".date" ).datepicker({ dateFormat: "yy-mm-dd" });
$(".time").mask("99:99");
$('.start_time').change(function(e) {
calcTotal(e);
});
$('.end_time').change(function(e) {
calcTotal(e);
});
$('.pause').change(function(e) {
calcTotal(e);
});
});
function calcTotal(event)
{ var myId = event.currentTarget.id;
myId = myId.replace(/[^0-9]/g, '');
var timeRegex = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
if($('#start'+myId).val().match(timeRegex) && $('#end'+myId).val().match(timeRegex) && $('#pause'+myId).val().match(timeRegex))
{
var minutes = 0;
var n = $('#end'+myId).val().split(':');
minutes = parseInt(n[0])*60 + parseInt(n[1]);
var n = $('#start'+myId).val().split(':');
minutes -= parseInt(n[0])*60 + parseInt(n[1]);
var n = $('#pause'+myId).val().split(':');
minutes -= parseInt(n[0])*60 + parseInt(n[1]);
var hours = Math.floor(minutes/60);
minutes = minutes % 60;
alert(hours + ':' + minutes);
$('#total' + myId).val(hours + ':' + minutes);
}
else
{ alert($('#start'+myId).val());
$('#total' + myId).val('00:00');
}
}
</script>
<div class="inner_container">
<form name="input" method="POST" id="input">
<table border="1">
<tr>
<td>Date</td>
<td>Start time</td>
<td>End time</td>
<td>Pause</td>
<td>Total</td>
</tr>
<tr>
<?php for($i = 0;$i < $rows;$i++)
{?>
<td><input type="text" class="date" name="date[]" id="start<?php echo $i;?>"></td>
<td><input type="text" class="start_time time" name="start_time[]" id="start<?php echo $i;?>"></td>
<td><input type="text" class="end_time time" name="end_time[]" id="end<?php echo $i;?>" ></td>
<td><input type="text" class="pause time" name="pause[] " id="pause<?php echo $i;?>"></td>
<td><input type="text" class="total" name="total[]" id="total<?php echo $i;?>"></td>
</tr>
<?php }?>
</table>
<input type="submit" id="submit_inner_container"value="Submit">
</form>
<form name="amount" method="POST">
<label for="name">How many rows would you like?</label>
<input type="text" name="rows" size="3" id="rows" value="<?php echo $rows; ?>"/>
<input type="submit" value="Rows"
</form>
</div>