我if
有条件问题。我写了一个脚本,alert
当字段#customer-age
值小于 21(计算的人的年龄)时,它应该显示。问题是 - 每次都会显示警报 - 当值小于和大于 21 时。
我的html代码是:
<div class="type-text">
<label for="birthday">Date1:</label>
<input type="text" size="20" id="birthday" name="birthday" value="" readonly="readonly" />
</div>
<div class="type-text">
<span id="customer-age" readonly="readonly"></span>
</div>
<span id="date_from_start">23/11/2012</span>
和脚本看起来像:
function getAge() {
var sday = $('#date_from_start').html();
var split_date1 = sday.split("/");
var todayDate = new Date(split_date1[2],split_date1[1] - 1,split_date1[0]);
var bday = $('#birthday').val();
var split_date2 = bday.split("/");
var birthDate = new Date(split_date2[2],split_date2[1] - 1,split_date2[0]);
var age = todayDate.getFullYear() - birthDate.getFullYear();
var m = todayDate.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && todayDate.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
var startDate = new Date("1935,01,01");
$('#birthday').datepicker({
dateFormat: 'dd/mm/yy',
dayNamesMin: ['Nie', 'Pon', 'Wt', 'Śr', 'Czw', 'Pt', 'Sob'],
dayNames: ['Niedziela','Poniedzialek','Wtorek','Środa','Czwartek','Piątek','Sobota'],
monthNamesShort: ['Sty', 'Lut', 'Mar', 'Kwi', 'Maj', 'Cze', 'Lip', 'Sie', 'Wrz', 'Paź', 'Lis', 'Gru'],
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
constrainInput: true, firstDay: 1, dateFormat: 'dd/mm/yy',
yearRange: '-77:-18',
defaultDate: startDate,
onSelect: function(dateText, inst) {
$('#customer-age').html(getAge(new Date(dateText)));
var cage = $('#customer-age').val();
if (cage < 21)
{
alert('< 21 year');
}
else {
}
},
maxDate: +0
});