如果填充了其中一个过滤器(from,to),我想用必需属性创建过滤日期(from和to)。我正在使用jquery datepicker进行日期选择。
这是我的代码:
$(document).on('change', "#from", function () {
if ($('#to').val().length == 0 && $('#from').val().length == 0) {
$('#from').removeAttr('required');
$('#to').removeAttr('required');
} else if ($('#to').val().length != 0 || $('#from').val().length != 0) {
$('#from').attr("required", "true");
$('#to').attr("required", "true");
}
});
$(document).on('change', "#to", function () {
if ($('#to').val().length == 0 && $('#from').val().length == 0) {
$('#from').removeAttr('required');
$('#to').removeAttr('required');
} else if ($('#to').val().length != 0 || $('#from').val().length != 0) {
$('#from').attr("required", "true");
$('#to').attr("required", "true");
}
});
这是文本框的代码:
@Html.TextBoxFor(modelitem => Model.startdate, new { @class = "datepicker form-control", @id = "from", @placeholder = "Select From Date" })
@Html.TextBoxFor(modelitem => Model.finishdate, new { @class = "datepicker form-control", @id = "to", @placeholder = "Select To Date" })
如果我在from和to文本框中输入一些文本,这段代码工作正常。 但是,它不适用于datepicker。我不明白为什么从datepicker中选择的日期不会触发javascript。谁知道为什么?