0

当我计算开始日期和结束日期之间的差异时 - 可以将结束日期输入为开始日期“之前” - 因此允许“总天数”中的负计算值。)我使用“分钟”来确保开始日期不能是“今天之前”。)

此外,是否有可能在计算完成之前隐藏“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>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</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>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</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>
4

3 回答 3

1

只需将您的 UI 更新包装在 if 语句中

if( days >= 0 ) {
    document.getElementById('total_days').value = days;
} else {
    alert( "Nice try, but you can't go backwards in time" );
}
于 2012-05-30T19:58:27.303 回答
1

当您搜索数字的绝对值(不带符号的值)时,请使用Math.abs.

于 2012-05-30T19:58:45.053 回答
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);

});
于 2012-05-30T20:06:14.853 回答