当我计算开始日期和结束日期之间的差异时 - 可以将结束日期输入为“开始日期之前” - 因此允许“totaldays”中的负计算值。 )我使用“min”来确保开始日期不能“在今天之前”。 )
另外,是否有可能在计算完成之前隐藏“total_days”结果(计算结果) - 即,这样就不会看到长十进制值:例如-15489.916666666666
我试图自己完成这个,但javascript / jquery知识非常有限。我会重视任何回应。 :-)谢谢
<title>Conference Form</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jqueryui.js"></script>
<link href="style.css" rel="stylesheet" type="text/css"/>
<link href="css/jqueryui.css" rel="stylesheet" type="text/css" />
<body>
<div id="wrapper">
<div id="header"></div>
<table width="705" border="1" align="left" cellpadding="1">
<form action="" method="get" name="myform">
<tr>
<td width="151"><label>Company Name</label></td>
<td width="158"><input type="text" name="companyname" id="companyname" /></td>
<td width="155">Enquiry Date</td>
<td width="213"><input type="text" name="enquiry_date" id="enquiry_date" class="datepicker" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Conference Date In</td>
<td><input type="text" name="conference_date_in" id="conference_date_in" class="datepicker" /></td>
<td>Conference Date Out</td>
<td><input type="text" name="conference_date_out" id="conference_date_out" class="datepicker" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Number of Delegates</td>
<td><input type="text" name="no_of_delegates" id="no_of_delegates" /></td>
<td>Total Days</td>
<td><input type="text" name="total_days" id="total_days" /></td>
</tr>
</form>
</table>
</div>
</body>
</html>
<script type="text/javascript">
$(function() {
$(".datepicker").datepicker({ minDate: -0, maxDate: "+100M +10D",dateFormat: 'dd-mm-yy'})
({
changeMonth: true,
changeYear: true,
});
});
var enquiry_date = $.datepicker.formatDate('dd-mm-yy', new Date());
document.getElementById('enquiry_date').value = enquiry_date;
var calcDate = function() {
var start = $('#conference_date_in').datepicker('getDate');
var end = $('#conference_date_out').datepicker('getDate');
var days = (end - start) / 1000 / 60 / 60 / 24;
document.getElementById('total_days').value = days;
}
$('#conference_date_out').change(calcDate);
$('#conference_date_in').change(calcDate);
</script>
答案 0 :(得分:1)
将您的UI更新包装在if语句中
if( days >= 0 ) {
document.getElementById('total_days').value = days;
} else {
alert( "Nice try, but you can't go backwards in time" );
}
答案 1 :(得分:1)
当您搜索数字的绝对值(没有符号的值)时,请使用Math.abs
。
答案 2 :(得分:0)
$(document).ready(function() {
$(".datepicker").datepicker({ minDate: -0, maxDate: "+100M +10D",dateFormat: 'dd-mm-yy', changeMonth: true, changeYear: true });
var enquiry_date = $.datepicker.formatDate('dd-mm-yy', new Date());
$('#enquiry_date').val(enquiry_date);
var calcDate = function() {
var start = $('#conference_date_in').datepicker('getDate');
var end = $('#conference_date_out').datepicker('getDate');
if (start << end) {
var days = (end - start) / 1000 / 60 / 60 / 24;
$('total_days').val(days);
} else {
alert('Day Out must be after the Day In. Try again.');
}
}
$('#conference_date_out').change(calcDate);
$('#conference_date_in').change(calcDate);
});