【问题标题】:Javascript redirect URLJavascript 重定向网址
【发布时间】:2016-08-24 20:31:18
【问题描述】:

下面是我使用的与网站表单相关的一些脚本。如果前两个函数无效,我正在尝试让它重定向到特定页面。

发生的事情是即使函数有效,也会发生重定向

我确定我在这里遗漏了一些非常简单的东西......

任何帮助表示赞赏!

(function(){
var f1 = fieldname2,
    valid_pickup_postcode = function (postcode) {
    postcode = postcode.replace(/\s/g, "");
    var regex = /^[O,X]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
    return regex.test(postcode);
    };
var f2 = fieldname7,
    valid_dropoff_postcode = function (postcode) {
    postcode = postcode.replace(/\s/g, "");
    var regex = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
    return regex.test(postcode);
};

if( AND(f1,f2))
{
   if( valid_pickup_postcode(f1) && valid_dropoff_postcode(f2))
   {
      return 'Please select the vehicle you require for your delivery';
   }
   else
   {
      return window.location.href = "http://www.bing.com";
   }
}
else
{
return '';
}
})()

【问题讨论】:

  • AND() 函数是什么?
  • 你在哪里设置fieldname2fieldname7
  • 由于这是一个 IIFE,它会在页面加载时立即运行,而不是在用户填写任何输入字段后运行。那么您希望它如何验证用户输入呢?
  • AND 函数似乎不是来自 javascript,将多个值分配给 f1,f2。你检查语法错误了吗?
  • 正则表达式变量应该是字符串吗?

标签: javascript java url if-statement url-redirection


【解决方案1】:
(function() {
    var f1 = fieldname2,
        valid_pickup_postcode = function(postcode) {
            postcode = postcode.replace(/\s/g, "");
            var regex = /^[O,X]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
            return regex.test(postcode);
        };
    var f2 = fieldname7,
        valid_dropoff_postcode = function(postcode) {
            postcode = postcode.replace(/\s/g, "");
            var regex = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
            return regex.test(postcode);
        };

    if (AND(f1, f2)) {
        if (valid_pickup_postcode(f1) && valid_dropoff_postcode(f2)) {
            return 'Please select the vehicle you require for your delivery';
        } else {
            // return window.location.href = "http://www.bing.com";
            window.location.replace("http://www.bing.com");
        }
    } else {
        return '';
    }
})()

window.location.replace("http://www.bing.com"); 应该可以解决问题

更新:我做了一些小改动以使您的代码正常工作。对于像验证取件和投递邮政编码一样简单的事情,JS 不是(或不应该)非常复杂:) 这是一个更简单的版本

function myValidator(f1, f2) {
    // Validate pickup postal code
    function pickup_postcode(postcode) {
        if (postcode) {
            if (isNaN(postcode)) {
                postcode = postcode.replace(/\s/g, "");
                var regex = /^[O,X]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
                return regex.test(postcode);
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    // Validate dropoff postal code
    function dropoff_postcode(postcode) {
        if (postcode) {
            if (isNaN(postcode)) {
                postcode = postcode.replace(/\s/g, "");
                var regex = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
                return regex.test(postcode);
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    if (pickup_postcode(f1) === true && dropoff_postcode(f2) === true) { // If both pickup and dropoff postal codes are ok return a message prompting vehicle selection
        return 'Please select the vehicle you require for your delivery';
    } else { // Invalid pickup or dropoff postal code
        // Redirect to website because either pickup or dropoff postal code is invalid
        window.location.replace("https://www.bing.com");
    }
}

myValidator("X909EF", "X909EE"); // Call it this way

【讨论】:

  • 谢谢,但是我已经尝试了 window.location 的所有变体,它仍然会自动重定向,似乎是因为它是一个 IIFE。我不明白为什么'else'函数不能控制它!
  • 在我之前的回答中稍微修改了代码。这应该工作
猜你喜欢
  • 2010-11-29
  • 1970-01-01
  • 2014-06-08
  • 2011-05-03
  • 2017-10-21
  • 2010-12-31
相关资源
最近更新 更多