最初我已禁用To date
字段,选择From date
后我只想让它选择。我为此编写了代码,但它没有用。
$(document).ready(function() {
$('#levTo').attr('disabled', 'true');
$('#levFrom').change(function() {
$('#levTo').attr('disabled', 'false');
})
pastDaysdisable()
});
function pastDaysdisable() {
var dtToday = new Date();
var month = dtToday.getMonth() + 1;
var day = dtToday.getDate();
var year = dtToday.getFullYear();
if (month < 10)
month = '0' + month.toString();
if (day < 10)
day = '0' + day.toString();
var maxDate = year + '-' + month + '-' + day;
$('#levFrom,#levTo').attr('min', maxDate);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
<label class="col-xs-12">Date From</label>
<input type="date" id="levFrom" class="col-xs-12 form-control levFrom required">
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
<label class="col-xs-12">Date To</label>
<input type="date" id="levTo" class="col-xs-12 form-control levTo required">
</div>
&#13;
另外,我正在尝试根据To date
来限制From date
To date
不应该允许选择我选择的from date
之前的日期。如何做?
我知道它可能在日期选择器中。但是,我想在input type=date.
答案 0 :(得分:2)
使用$('#levTo').prop('disabled', false);
使用prop()方法在使用jQuery时启用或禁用元素
$(document).ready(function() {
$('#levTo').attr('disabled', 'true');
$('#levFrom').change(function() {
$('#levTo').prop('disabled', false);
pastDaysdisable($('#levFrom').val())
})
});
function pastDaysdisable(date) {
$('#levFrom,#levTo').attr('min', date);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
<label class="col-xs-12">Date From</label>
<input type="date" id="levFrom" class="col-xs-12 form-control levFrom required">
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
<label class="col-xs-12">Date To</label>
<input type="date" id="levTo" class="col-xs-12 form-control levTo required">
</div>
答案 1 :(得分:0)
尝试使用:
$('#levFrom').on('change keyup paste',function (){
$('#levTo').removeAttr('disabled');
})
而不是:
$('#levFrom').change(function() {
$('#levTo').attr('disabled', 'false');
})