[英]how to create Multiple instances on the same page of js function
考慮以下代碼片段:
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 function 來正確執行?
<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 function的同一頁面上創建多個實例?
您必須遍歷包含 2 個輸入的每個表單。
例如,您可以將 class 添加到表單而不是 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);
}
}
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.