我有一个国家的下拉列表:
<select id="country" name="country" aria-disabled="false">
<option value="AU" age="18" selected="selected">Australia</option>
<option value="US" age="21">United States</option>
<option value="UK" age="18">United Kingdom</option>
<option value="HK" age="19">Canada</option>
</select>
和3选择日、月和年由于某种原因,年龄门只计算年,而不计算日和月。因此,当该人的生日在年底时,表单将成功提交,而不是推送Age通知。不能得到什么我做错了:
这是我的代码如下:
var t = function (t, e) {
return e.getFullYear() - t.getFullYear();
};
$.get("https://ipapi.co/country/", function (t) {
(clientCountry = "AU"), (clientCountry = t);
$("#country option[value=" + clientCountry + "]").val();
$("#country option[value=" + clientCountry + "]").attr("selected", "selected");
var e = $("#country option:selected").attr("age");
$("#age-gate__country .ui-selectmenu-status").text($("select[name=country] option:selected").text());
var age = new Date(new Date().setFullYear(new Date().getFullYear() - e)).toLocaleDateString("en-US");
$("#targetAge").text(age),
"US" === clientCountry || "UK" === clientCountry
? ($(".age-gate__content__options").addClass("visible"), $(".age-gate__content__options").removeClass("visible"))
: ($(".age-gate__content__options").removeClass("visible"), $(".age-gate__content__options").addClass("visible"));
}),
$("#ageGateAdvancedForm").submit(function (e) {
e.preventDefault();
var age = $("#country option:selected").attr("age"),
date = $("#v65-billBirthDay option:selected").val(),
month = $("#v65-billBirthMonth option:selected").val(),
year = $("#v65-billBirthYear option:selected").val(),
today = new Date(),
// s = new Date(),
birth = new Date(year, month, date),
c = t(birth, today);
c >= age && c < 100 ? ($(".age-gate").removeClass("age-gate__active"), vtScripts.cookies("ageGate", "remember", 365)) : $("#agegate-error").text("You must be older than " + age + "!");
});
更新:以下是我编辑
下面代码的方法$("#ageGateAdvancedForm").submit(function (e) {
e.preventDefault();
var a = $("#country option:selected").attr("age"),
i = $("#v65-billBirthDay option:selected").val(),
n = $("#v65-billBirthMonth option:selected").val(),
o = $("#v65-billBirthYear option:selected").val(),
s = new Date(),
r = new Date(o, n - 1, i),
c = t(r, s);
const dt = new Date();
dt.setFullYear(dt.getFullYear() - a);
c = dt.getTime() - new Date(r).getTime();
c >= a ? ($(".age-gate").removeClass("age-gate__active"), vtScripts.cookies("ageGate", "remember", 365)) : $("#agegate-error").text("You must be older than " + a + "!");
});
解决方案:
$("#ageGateAdvancedForm").submit(function (e) {
e.preventDefault();
var a = $("#country option:selected").attr("age"),
i = $("#v65-billBirthDay option:selected").val(),
n = $("#v65-billBirthMonth option:selected").val(),
o = $("#v65-billBirthYear option:selected").val(),
s = new Date(),
r = new Date(o, n - 1, i),
c = t(r, s);
const dt = new Date();
dt.setFullYear(dt.getFullYear() - a);
c = dt.getTime() - new Date(r).getTime();
c >= a ?
($(".age-gate").removeClass("age-gate__active"), vtScripts.cookies("ageGate", "remember", 365))
: $("#agegate-error").text("You must be older than " + a + "!");
});