请考虑以下代码段:
var from,to;
to = $(".range-to-dt").persianDatepicker({
inline: true,
minDate: new persianDate(cleanDate(serverDateTime)),
altField: '.range-to-dt-alt',
altFormat: 'YYYY/MM/DD',
initialValue: false,
onSelect: function (unix) {
to.touched = true;
if (from && from.options && from.options.maxDate != unix) {
var cachedValue = from.getState().selected.unixDate;
from.options = { maxDate: unix };
if (from.touched) {
from.setDate(cachedValue);
}
}
}
});
from = $(".range-from-dt").persianDatepicker({
inline: true,
observer: true,
minDate: new persianDate(cleanDate(serverDateTime)),
altField: '.range-from-dt-alt',
altFormat: 'YYYY/MM/DD',
initialValue: false,
onSelect: function (unix) {
from.touched = true;
if (to && to.options && to.options.minDate != unix) {
var cachedValue = to.getState().selected.unixDate;
to.options = { minDate: unix };
if (to.touched) {
to.setDate(cachedValue);
}
}
}
});
如何在同一个页面上多次使用js函数(以几种不同的形式)才能正确执行?
<form id="form1" ...>
<input asp-for="DateTimeRange.StartDate" ltr-input range-from-dt-alt">
<input asp-for="DateTimeRange.EndDate" ltr-input range-to-dt-alt">
</form>
<form id="form2" ...>
<input asp-for="DateTimeRange.StartDate" ltr-input range-from-dt-alt">
<input asp-for="DateTimeRange.EndDate" ltr-input range-to-dt-alt">
</form>
如何在js函数的同一页面上创建多个实例呢?
您必须循环遍历包含2个输入的每个表单。
您可以向表单中添加一个类而不是ID例如,如
<form class="startEndForm">
// Your 2 inputs here
</form>
然后做这样的事情:
$('.startEndForm').each(function () {
$(this).find(".range-to-dt").persianDatepicker({
inline: true,
minDate: new persianDate(cleanDate(serverDateTime)),
altField: '.range-to-dt-alt',
altFormat: 'YYYY/MM/DD',
initialValue: false,
onSelect: function (unix) {
var from = $(this).parent().find('.range-from-dt');
var to = $(this);
to.touched = true;
if (from && from.options && from.options.maxDate != unix) {
var cachedValue = from.getState().selected.unixDate;
from.options = { maxDate: unix };
if (from.touched) {
from.setDate(cachedValue);
}
}
}
});
$(this).find(".range-from-dt").persianDatepicker({
inline: true,
observer: true,
minDate: new persianDate(cleanDate(serverDateTime)),
altField: '.range-from-dt-alt',
altFormat: 'YYYY/MM/DD',
initialValue: false,
onSelect: function (unix) {
var from = $(this);
var to = $(this).parent().find('.range-to-dt');
from.touched = true;
if (to && to.options && to.options.minDate != unix) {
var cachedValue = to.getState().selected.unixDate;
to.options = { minDate: unix };
if (to.touched) {
to.setDate(cachedValue);
}
}
}
});
});