JavaScript警告弹出两次

4

我正在尝试在Drupal 7中使用一个名为“disclaimer”的模块,但是警告“您必须输入您出生年份。”弹出两次,然后重定向到您不应该看到的URL,直到您验证自己已经超过18岁。

我尝试了建议,但它仍然出现了两次。我认为问题可能是输入按钮的操作。以下是该函数:

 Drupal.behaviors.disclaimer = {
  attach: function (context, settings) {
    // action on enter button
    $('#disclaimer_enter', context).click(function () {
      var check = true;
      // age form is set
      if (settings.disclaimer.ageform == 1) {
        var check = Drupal.checkAge();
      }
      // everything good, add cookie and close colorbox
      if (check) {
        $.cookie(settings.disclaimer.cookie_name, '1', { path: settings.disclaimer.cookie_path });
        $.colorbox.remove();
      }
    });
  },
};

}

    Drupal.checkAge = function () {
    var now = new Date();
    var date = now.getDate();
    var month = now.getMonth() + 1;
    var year = now.getFullYear();
    var optmonth = jQuery("#edit-disclaimer-age-month option:selected").val();
    var optday = jQuery("#edit-disclaimer-age-day option:selected").val();
    var optyear = jQuery("#edit-disclaimer-age-year option:selected").val();
    var age = year - optyear;
    if (optmonth > month) {age--;} else {if(optmonth == month && optday >= date) {age--;}}
    // if current year, form not set
    if (optyear == year) {
      alert(Drupal.t("You must enter the year you were born in."));
      return alert;
    // if under age, alert and redirect !
    } else if (age < Drupal.settings.disclaimer.limit) {
      alert(Drupal.t("Sorry, you are Under age limit and are prohibited from entering this site!"));
      location.replace(Drupal.settings.disclaimer.exiturl);
      return false;
    } else {
      // age limit ok
      return true;
    }
  }

4
首先,“return alert;” 可能不是你想要做的。 - John Conde
2个回答

8

因为您返回了一个警报。这将显示警报两次,并且因为您没有返回false,它将继续到下一页。

尝试这个:

if (optyear == year) {
    alert(Drupal.t("You must enter the year you were born in."));
    return false;
}

我尝试过了,它仍然出现了两次。我已经在“Enter”按钮上方添加了代码,这也可能是问题所在。 - user2316905
@user2316905 你需要确保它也会返回 false - Mathew Thompson

1

return alert 不是必需的。

alert(Drupal.t("You must enter the year you were born in."));
//return alert;

警告框可以通过使用alert()函数触发。

我尝试了一下,但它仍然出现了两次,并重定向到只有在您将年龄设置为18岁以上时才能看到的URL。 - user2316905

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接