谁能帮我根据年龄重定向?
Can anyone help me in redirect based on age?
有一个年龄选择器。
如果 18-31 y/old >按钮 > link 到一个网站......
否则 32-99 y/old > 相同的按钮 >link 到另一个另一个
可能的代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Add your Age</title>
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.0/jquery-ui.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css">
<script>
$( function() {
$('#datepicker').datepicker({
changeMonth:true,
changeYear:true,
yearRange:"1920:2021"
});
$('#datepicker').datepicker('show');
} );
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
<button style="background: orange; border: none;padding: 10px 25px;margin-left:40px;">Ok</button>
</body>
</html>
var link = document.getElementById('link');
var age = document.getElementById('ageInput');
var selectedAge;
age.addEventListener('change', function() {
if (this.value === '18-31') {
link.text = 'Foo';
link.href = '/foo';
} else {
link.text = 'Bar';
link.href = '/bar';
}
});
a {
display: block;
}
<label>
Select age:
<select id="ageInput">
<option value="18-31">18-31</option>
<option value="32-99">32-99</option>
</select>
<a id="link" href=""></a>
考虑以下示例。
$(function() {
var dateFormat = "mm/dd/yy";
var age = 0;
$('#datepicker').datepicker({
changeMonth: true,
changeYear: true,
yearRange: "1920:2021",
onSelect: function(dateText, uiObject) {
var now = new Date();
var birth = $.datepicker.parseDate("mm/dd/yy", dateText);
age = now.getFullYear() - birth.getFullYear();
}
});
$('#datepicker').datepicker('show');
$("button").click(function(event) {
event.preventDefault();
if (age > 17 && age < 32) {
console.log("Age 1:", age);
// Redirect to Page 1
}
if (age > 31) {
console.log("Age 2:", age);
// Redirect to Page 2
}
})
});
<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.0/themes/ui-lightness/jquery-ui.css" />
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.0/jquery-ui.js"></script>
<p>Birth Date: <input type="text" id="datepicker"></p>
<button class="ui-button ui-corner-all ui-priority-primary">Ok</button>
这会根据用户选择设置一个 age
变量。然后,您可以根据 age
.
有条件地重定向
查看更多:
有一个年龄选择器。
如果 18-31 y/old >按钮 > link 到一个网站......
否则 32-99 y/old > 相同的按钮 >link 到另一个另一个
可能的代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Add your Age</title>
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.0/jquery-ui.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css">
<script>
$( function() {
$('#datepicker').datepicker({
changeMonth:true,
changeYear:true,
yearRange:"1920:2021"
});
$('#datepicker').datepicker('show');
} );
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
<button style="background: orange; border: none;padding: 10px 25px;margin-left:40px;">Ok</button>
</body>
</html>
var link = document.getElementById('link');
var age = document.getElementById('ageInput');
var selectedAge;
age.addEventListener('change', function() {
if (this.value === '18-31') {
link.text = 'Foo';
link.href = '/foo';
} else {
link.text = 'Bar';
link.href = '/bar';
}
});
a {
display: block;
}
<label>
Select age:
<select id="ageInput">
<option value="18-31">18-31</option>
<option value="32-99">32-99</option>
</select>
<a id="link" href=""></a>
考虑以下示例。
$(function() {
var dateFormat = "mm/dd/yy";
var age = 0;
$('#datepicker').datepicker({
changeMonth: true,
changeYear: true,
yearRange: "1920:2021",
onSelect: function(dateText, uiObject) {
var now = new Date();
var birth = $.datepicker.parseDate("mm/dd/yy", dateText);
age = now.getFullYear() - birth.getFullYear();
}
});
$('#datepicker').datepicker('show');
$("button").click(function(event) {
event.preventDefault();
if (age > 17 && age < 32) {
console.log("Age 1:", age);
// Redirect to Page 1
}
if (age > 31) {
console.log("Age 2:", age);
// Redirect to Page 2
}
})
});
<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.0/themes/ui-lightness/jquery-ui.css" />
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.0/jquery-ui.js"></script>
<p>Birth Date: <input type="text" id="datepicker"></p>
<button class="ui-button ui-corner-all ui-priority-primary">Ok</button>
这会根据用户选择设置一个 age
变量。然后,您可以根据 age
.
查看更多: