Javascript警报显示两次

Javascript alert shows up twice

本文关键字:两次 显示 Javascript      更新时间:2023-09-26

我试图在drupal7中使用一个名为免责声明的模块,但是警告"您必须输入您出生的年份"。显示两次,然后继续重定向到你不应该看到的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;
    }
  }

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

试试这个:

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

返回警告是不必要的。

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

只需使用alert();