gpt4 book ai didi

javascript - 防止jquery计算后出现负值

转载 作者:行者123 更新时间:2023-12-02 19:37:06 30 4
gpt4 key购买 nike

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

另外,是否可以隐藏“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>

最佳答案

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

if( days >= 0 ) {
document.getElementById('total_days').value = days;
} else {
alert( "Nice try, but you can't go backwards in time" );
}

关于javascript - 防止jquery计算后出现负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10823253/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com