Javascript 使用单个函数查找两个日期之间的差异
在这段代码中,我试图获得两个日期和时间之间的差异 ex(09/05/2014 09:10:00-09/05/2014 11:18:00)对于多个文本框,它应该返回78分钟的单个功能,但不幸的是,这段代码不起作用。请有人建议我正确的代码,或者是否有其他方法执行此功能,提前感谢Javascript 使用单个函数查找两个日期之间的差异,javascript,jquery,jsp,Javascript,Jquery,Jsp,在这段代码中,我试图获得两个日期和时间之间的差异 ex(09/05/2014 09:10:00-09/05/2014 11:18:00)对于多个文本框,它应该返回78分钟的单个功能,但不幸的是,这段代码不起作用。请有人建议我正确的代码,或者是否有其他方法执行此功能,提前感谢 <html> <head> <title>TODO supply a title</title> <meta charset="UTF-8"> <meta n
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="JS/datejquery.min.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
$("id^='endTime'".change(function(){
var index = $(this).attr('id').replace('endTime','');
alert(index);
var startTime = $('#startTime'+index).val();
var endTime = $(this).val();
$('#result'+index).val(endTime-startTime);
});
} );
</script>
</head>
<body>
<form name="wrs">
<input type="text" name="time1" id= "startTime1" class="num-box type1" />
<input type="text" name="time2" id = "endTime1" class="num-box type2" />
<input type="text" name="result1" class="num-box type5" />
<input type="text" name="startTime2" id= "time3" class="num-box type3" />
<input type="text" name="endTime2" id = "time4" class="num-box type4" />
<input type="text" name="result2" class="num-box type6" />
</form>
</body>
</html>
提供头衔
$(文档).ready(函数(){
$(“id^='endTime'.change(function()){
var index=$(this.attr('id').replace('endTime','');
警报(索引);
var startTime=$('#startTime'+index).val();
var endTime=$(this.val();
$(“#结果”+索引).val(结束时间开始时间);
});
} );
您错过了括号
$("[id^='endTime']").change(function(){
var index = $(this).attr('id').slice(7);
alert(index);
var startTime = $('#startTime'+index).val();
var endTime = $(this).val();
$('#result'+index).val(endTime-startTime);
});
谢谢sudharsan,它正在工作,但值返回为NaN wat,我必须使用parseInt使其成为valuetry,如$('result'+index).val(parseInt(endTime,10)-parseInt(startTime,10));