【问题标题】:Can anyone help me in redirect based on age?任何人都可以根据年龄帮助我重定向吗?
【发布时间】:2022-01-11 05:01:35
【问题描述】:

有一个年龄选择器。

如果 18-31 岁/岁 >按钮 > 链接到网站.......

Otherwish 32-99 y/old > 相同的按钮 > 链接到另一个

可能的代码:

<!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>

【问题讨论】:

    标签: jquery jquery-ui


    【解决方案1】:

    考虑以下示例。

    $(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 有条件地重定向。

    查看更多:

    【讨论】:

    • 解决了,谢谢兄弟
    • @dipbarman 很高兴听到这个消息。您可以通过单击复选标记将其标记为答案。稍后,如果您选择,您也可以为答案投票。
    • 这个文件是index.html,如何在(重定向到第2页)代码处添加index2.html的位置并重定向这个页面。
    • @dipbarman 您的问题是关于如何根据特定的选定日期执行操作。我已经解决了这个问题。我会建议window.location.href="" 之类的东西,或者您可以自己尝试一下。如果您需要更多帮助,请搜索类似帖子或发布新问题。
    • 我做到了,亲爱的。再次感谢您
    【解决方案2】:

    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>

    【讨论】:

    • 谢谢,它成功了。但我需要一点不同。请检查我添加的代码。
    猜你喜欢
    • 1970-01-01
    • 2019-10-12
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多