2013-04-24 59 views
3

我想在Drupal 7中使用一個名爲disclaimer的模塊,但警報「您必須輸入您出生的年份。」出現兩次,然後繼續重定向到您不應該看到的網址,直到您確認您已超過18個。Javascript alert顯示兩次

我嘗試了這些建議,但它仍然顯示了兩次。我認爲問題可能是輸入按鈕操作。這是這個功能。

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

初學者'返回警報;'可能不是你想要做的 – 2013-04-24 18:42:44

回答

7

因爲你返回警報。這將顯示警報兩次,因爲你沒有返回false,將使其繼續到下一頁。

試試這個:

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

我試過了,它仍然出現了兩次。我爲上面的代碼添加了Enter按鈕,這也可能是問題所在。 – user2316905 2013-04-24 19:31:18

+0

@ user2316905您需要確保''返回false'。 – mattytommo 2013-04-24 21:11:33

1

回報警報時並不需要。

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

只需使用alert()函數即可解除警報。

+0

我試了一下,它仍然出現兩次,並且只有當您將您的年齡設置爲18歲以上時,纔會重定向到您應該看到的URL。 – user2316905 2013-04-24 19:39:38