jQuery More&少于

时间:2015-07-01 15:02:10

标签: javascript jquery

如果值等于或大于最小值且小于最大设定值,我试图显示警报。如果我将dob设置为' 01-09-1996'警报仍然被触发?有人会指出我做错了什么吗?谢谢。

$(function() {
    $('#date1 input').autotab_magic().autotab_filter('numeric');

    $(".eligibilityform").submit(function(e) {

        // Input Values
        var day = $("#day").val();
        var month = $("#month").val();
        var year = $("#year").val();

        // DOB Value
        var dob = new Date(day + '-' + month + '-' + year);

        // DOB Rules
        var mindob = new Date("01-09-1996");
        var maxdob = new Date("31-08-2000");

        // DOB Eligibility
        if (dob >= mindob && dob <= maxdob) {
            alert("Welcome");

            // Season Eligibility
            var spring2015min = new Date("01-09-1997");
            var spring2015max = new Date("31-08-1998");
            if (dob >= spring2015min && dob <= spring2015max) {
                alert("Spring 2015");
            }
            var summer2015min = new Date("01-09-1997");
            var summer2015max = new Date("31-08-1999");
            if (dob >= summer2015min && dob <= summer2015max) {
                alert("Summer 2015");
            }
            var autumn2015min = new Date("01-01-1998");
            var autumn2015max = new Date("31-08-1999");
            if (dob >= autumn2015min && dob <= autumn2015max) {
                alert("Autumn 2015");
            }
            var spring2016min = new Date("01-09-1998");
            var spring2016max = new Date("31-08-1999");
            if (dob >= spring2016min && dob <= spring2016max) {
                alert("Spring 2016");
            }
            var summer2016min = new Date("01-09-1998");
            var summer2016max = new Date("31-08-2000");
            if (dob >= summer2016min && dob <= summer2016max) {
                alert("Summer 2016");
            }
            var autumn2016min = new Date("01-01-1998");
            var autumn2016max = new Date("31-08-2000");
            if (dob >= autumn2016min && dob <= autumn2016max) {
                alert("Autumn 2016");
            }

        } else {
            alert("Sorry");
        }

        return false;
    });
});

回答

// Eligibility
$(function() {
  $('#date1 input').autotab_magic().autotab_filter('numeric');
  $('.error-message').hide();
  $('.success-message').hide();
  $('.seasons').hide();
  $('.seasons li').hide();
  $(".eligibilityform").submit(function(e){
    // Input Values
    var day = $("#day").val();
    var month = $("#month").val();
    var year = $("#year").val();
    // DOB Value
    var dob = new Date(month+'/'+day+'/'+year);
    // DOB Rules
    var mindob = new Date("09/01/1997");
    var maxdob = new Date("08/31/2000");
    // DOB Eligibility
    if (dob >= mindob && dob <= maxdob) {
      $('.success-message').show();
      $('.intro-message').hide();
      $('.error-message').hide();
      $('.eligibilityform').hide();
      $('.seasons').show();
      // Season Eligibility
      var spring2015min = new Date("09/01/1997");
      var spring2015max = new Date("08/31/1998");
      if (dob >= spring2015min && dob <= spring2015max){
              $('.seasons li.one').show();
      }
      var summer2015min = new Date("09/01/1997");
      var summer2015max = new Date("08/31/1999");
      if (dob >= summer2015min && dob <= summer2015max){
        $('.seasons li.two').show();
      }
      var autumn2015min = new Date("01/01/1998");
      var autumn2015max = new Date("08/31/1999");
      if (dob >= autumn2015min && dob <= autumn2015max){
        $('.seasons li.three').show();
      }
      var spring2016min = new Date("09/01/1998");
      var spring2016max = new Date("08/31/1999");
      if (dob >= spring2016min && dob <= spring2016max){
        $('.seasons li.four').show();
      }
      var summer2016min = new Date("09/01/1998");
      var summer2016max = new Date("08/31/2000");
      if (dob >= summer2016min && dob <= summer2016max){
        $('.seasons li.five').show();
      }
      var autumn2016min = new Date("01/01/1998");
      var autumn2016max = new Date("08/31/2000");
      if (dob >= autumn2016min && dob <= autumn2016max){
        $('.seasons li.six').show();
      }
    } else {
      $('.intro-message').hide();
      $('.success-message').hide();
      $('.error-message').show();
    }
    return false;
  });
});

3 个答案:

答案 0 :(得分:1)

这是因为您尝试在>=上使用<=string运算符。

您需要将字符串转换为Date对象

var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

请参阅Here

然后您可以比较日期

  var dob = new Date("09-01-1996");
  var spring2015min = new Date("09-01-1997");
  var spring2015max = new Date("08-31-1998");
  if (dob >= spring2015min && dob <= spring2015max){
    alert("Spring 2015");
  }

<强>更新 在您的情况下,Date对象将格式字符串视为(MM-dd-yyyy)

您可以在此处看到它:JSFiddle

答案 1 :(得分:1)

问题是你无法比较日期,只有整数。我会用这样的东西:

if( (new Date(dob).getTime() >= new Date(spring2015min).getTime()))

答案 2 :(得分:0)

您正在进行的当前测试是字符串检查,因此它将测试实际字符串是否相等或其数字字符等价物是否大于其数值。您应该使用将字符串转换为日期 new Date()然后对日期对象进行检查