Javascript JQuery无法理解如何在多个选项选择中对这些日期进行排序
我正在尝试使用JQuery删除和排序多个下拉列表中的日期 基本上我们有课程日期,我不想展示任何已经通过的课程。所以我决定:Javascript JQuery无法理解如何在多个选项选择中对这些日期进行排序,javascript,html,jquery,date,sorting,Javascript,Html,Jquery,Date,Sorting,我正在尝试使用JQuery删除和排序多个下拉列表中的日期 基本上我们有课程日期,我不想展示任何已经通过的课程。所以我决定: 删除今天之前的任何日期 按剩余日期的日期顺序排序 我试图在一个函数中完成它,但失败了。我能在一次约会中做到这一点,但我不知道如何在多个下拉列表中循环。(我想我需要以某种方式绕过它们) 这是HTML(在本例中,只有两个日期的ID标签分别为pa_date_11和pa_date_12(数字随产品而变化,可能超过2)) 指甲技师套装 同时预订这两个热门课程,可节省25英镑。这个
指甲技师套装
同时预订这两个热门课程,可节省25英镑。这个软件包涵盖了两种最流行的指甲延伸方法。在这两天的课程中,你将学习紫外线凝胶延伸剂、紫外线凝胶抛光剂以及丙烯酸指甲延伸剂
紫外线凝胶抛光课程| 1/2天课程
这是在指甲上涂抹丝般光滑且持久的紫外线凝胶抛光剂的完美方法。凝胶上光剂是理想的沙龙护理,因为它只需30分钟即可上光,并提供长达三周的光泽漆面
课程地点
指甲延伸丙烯酸| 1天课程
这是一个为期一天的独家课程,涵盖丙烯酸延伸指甲应用的所有方面。您将应用自然外观的技巧,并学习如何将它们混合到自然指甲中,以创建完美的延伸
课程地点
指甲延长紫外线凝胶| 1天疗程
这是一个为期一天的独家课程,涵盖紫外线凝胶延伸指甲应用的所有方面。您将应用自然外观的技巧,并学习如何将它们混合到自然指甲中,以创建完美的延伸
请致电安排日期
此产品目前不可用,请致电我们安排日期。
到目前为止,这是我的JQuery
// remove any dates older than today function
(function ($) {
$.fn.removeOldDates = function () {
$("select[id^='pa_date_'] > option").each(function () {
//console.log(this.text + ' ' + this.value);
const now = new Date().getTime();
const time = this.text.split(/[\s/-]+/); // split the time, by / or -
const day = time[0];
const month = time[1] - 1;
const year = time[2];
//console.log(time);
const row_date = new Date();
const test = row_date.setFullYear(year, month, day);
// now we have both times, we can compare them
if (now > row_date.getTime() || this.text == "") {
// if the date is less that today or empty remove it from the dropdown
$("select[id^='pa_date_'] > option[value=" + this.value + "]").remove();
}
});
};
})(jQuery);
// re-order the dates function
(function ($) {
$.fn.orderDates = function () {
$(this)
.find("option:not(:first)")
.each(function (index, value) {
const t = this.textContent.split(/[\s/-]+/);
$(this).data("_ts", new Date(t[2], t[1] - 1, t[0]).getTime());
console.log(t);
$(this)
.sort(function (a, b) {
return $(a).data("_ts") - $(b).data("_ts");
})
.appendTo("#pa_date_12");
//$(this).html("select[id^=pa_date_]" + index);
//$(this).html( $('<option value="Loo">Loo' + index + '</option>') );
});
return this;
};
})(jQuery);
// Map the date drop downs
$("select[id^=pa_date_]").each(function (index) {
console.log(index);
// $(this) refers to one specific <select> element
// $(this).append('<option value="foo">Bar ' + index + '</option>');
$(this).removeOldDates();
$(this).orderDates();
});
//删除任何早于今天的日期函数
(函数($){
$.fn.removeOldDates=函数(){
$(“选择[id^='pa\U日期]]>选项”)。每个(函数(){
//console.log(this.text+''+this.value);
const now=new Date().getTime();
const time=this.text.split(//[\s/-]+/);//按/或分割时间-
常数天=时间[0];
常量月=时间[1]-1;
常数年=时间[2];
//console.log(时间);
const row_date=新日期();
const test=行\日期.setFullYear(年、月、日);
//现在我们有两个时间,我们可以比较它们
if(now>row_date.getTime()|| this.text==“”){
//如果日期小于今天或为空,则将其从下拉列表中删除
$(“选择[id^='pa_date']>选项[value=“+this.value+”])。删除();
}
});
};
})(jQuery);
//重新排序日期函数
(函数($){
$.fn.orderDates=函数(){
$(本)
.find(“选项:非(:第一个)”)
.每个(函数(索引、值){
const t=this.textContent.split(/[\s/-]+/);
$(this).data(“ts”,新日期(t[2],t[1]-1,t[0]).getTime();
控制台日志(t);
$(本)
.排序(功能(a、b){
返回$(a).data(“_ts”)-$(b).data(“_ts”);
})
.附于(“#pa#U日期#U 12”);
//$(this.html(“选择[id^=pa_date_]”+索引);
//$(this.html($('Loo'+index+'');
});
归还这个;
};
})(jQuery);
//映射日期下拉列表
$(“选择[id^=pa_日期]”)。每个(函数(索引){
控制台日志(索引);
//$(this)引用一个特定元素
//$(this.append('Bar'+index+'');
$(this.removeOldDates();
$(this.orderDates();
});
如果你想用叉子和锤子,我也会把它放在密码笔里
我被orderDates函数中的.sort方法卡住了,因为它返回NAN,然后将其附加到相关下拉列表中
感谢任何帮助我最终到达了那里,我关闭each(
函数()
)太晚了
这是我最后的jQuery:
// remove any dates older than today function
(function ($) {
$.fn.removeOldDates = function () {
$("select[id^='pa_date_'] > option").each(function () {
const now = new Date().getTime();
const time = this.text.split(/[\s/-]+/); // split the time, by / or -
const day = time[0];
const month = time[1] - 1;
const year = time[2];
const row_date = new Date();
const test = row_date.setFullYear(year, month, day);
// now we have both times, we can compare them
if (now > row_date.getTime() || this.text == "") {
// if the date is less that today or empty remove it from the dropdown
$("select[id^='pa_date_'] > option[value=" + this.value + "]").remove();
}
});
};
})(jQuery);
// re-order the dates function
(function ($) {
$.fn.orderDates = function () {
$(this).find("option:not(:first)")
.each(function (index, value) {
const t = this.textContent.split(/[\s/-]+/);
$(this).data( "_ts", new Date(t[2], t[1] - 1, t[0]).getTime() );
}).sort(function (a, b) {
return $(a).data("_ts") - $(b).data("_ts");
}).appendTo($(this));
return this;
};
})(jQuery);
// Map the date drop downs
$("select[id^=pa_date_]").each(function (index) {
$(this).removeOldDates();
$(this).orderDates();
});
我一定会再次访问wo
// remove any dates older than today function
(function ($) {
$.fn.removeOldDates = function () {
$("select[id^='pa_date_'] > option").each(function () {
const now = new Date().getTime();
const time = this.text.split(/[\s/-]+/); // split the time, by / or -
const day = time[0];
const month = time[1] - 1;
const year = time[2];
const row_date = new Date();
const test = row_date.setFullYear(year, month, day);
// now we have both times, we can compare them
if (now > row_date.getTime() || this.text == "") {
// if the date is less that today or empty remove it from the dropdown
$("select[id^='pa_date_'] > option[value=" + this.value + "]").remove();
}
});
};
})(jQuery);
// re-order the dates function
(function ($) {
$.fn.orderDates = function () {
$(this).find("option:not(:first)")
.each(function (index, value) {
const t = this.textContent.split(/[\s/-]+/);
$(this).data( "_ts", new Date(t[2], t[1] - 1, t[0]).getTime() );
}).sort(function (a, b) {
return $(a).data("_ts") - $(b).data("_ts");
}).appendTo($(this));
return this;
};
})(jQuery);
// Map the date drop downs
$("select[id^=pa_date_]").each(function (index) {
$(this).removeOldDates();
$(this).orderDates();
});