javascript - 如何使用 "JavaScript Switch Statement"重定向用户

标签 javascript jquery html

我正在使用区号来查找他们的省份并重定向到自定义页面。这是我的 JavaScript 代码,用于使用他们的区号找出他们的位置。

    function getProvince(pStrPhone) {
    var areacode = pStrPhone.substring(0, 3);
    switch (areacode) {
      case "403":
      case "780":
      case "587":
      case "825":
        return "AB";
      case "604":
      case "778":
      case "250":
      case "236":
        return "BC";
      case "204":
      case "431":
        return "MB";
      case "506":
        return "NB";
      case "709":
        return "NL";
      case "867":
        return "YT";
      case "902":
      case "782":
        return "NS";
      case "416":
      case "647":
      case "437":
      case "519":
      case "226":
      case "548":
      case "613":
      case "343":
      case "705":
      case "249":
      case "807":
      case "905":
      case "289":
      case "365":
        return "ON";
      case "418":
      case "581":
      case "450":
      case "579":
      case "514":
      case "438":
      case "819":
      case "873":
        return "QC";
      default:
        return "";
    }
  }  

这样,我将使用下面的代码将用户重定向到不同的页面。

function redirect() {
      var province = $('#sfstate').val();
        if (province == 'BC') {
            $("#retURL" ).val("https://test.cleardent.com/demo-thankyou-bc.html");
        } else {
      if (province == 'ON') {
        $("#retURL" ).val("https://test.cleardent.com/demo-thankyou-on.html");
    } else {  
        $("#retURL" ).val("https://test.cleardent.com/demo-thankyou-other.html");      
      }
    }
    }

现在,我想为 getProvince 函数添加“其他”并将用户重定向到默认页面,以防万一有人输入随机数字,例如 111-111-1111。

这是我的尝试。

    function redirect() {
      var province = $('#sfstate').val();
        if (province == 'BC') {
            $("#retURL" ).val("https://test.cleardent.com/demo-thankyou-bc.html");
        } else {
    if (province == 'ON') {
        $("#retURL" ).val("https://test.cleardent.com/demo-thankyou-on.html");
    } else {  
    if (province == 'AB','MB','NB','NL','YT','NS','QC') {
        $("#retURL" ).val("https://test.cleardent.com/demo-thankyou-other.html");    
    } else {  
        $("#retURL" ).val("https://test.cleardent.com/demo-thankyou-na.html");    
    } 
  }
    }
  }

由于某种原因这不起作用。各位大师,您能在这里发现我的错误吗?

非常感谢, 杰森

最佳答案

if (province == 'AB','MB','NB','NL','YT','NS','QC') {
    $("#retURL" ).val("https://test.cleardent.com/demo-thankyou-other.html");    
}

条件错误。您可以将值推送到数组中,并使用 indexOf 来查找至少与 province 完全匹配的值。

var otherLetters = ['AB','MB','NB','NL','YT','NS','QC'];
if (otherLetters.indexOf(province) > -1) {
    $("#retURL" ).val("https://test.cleardent.com/demo-thankyou-other.html");    
}

完整代码如下;

function redirect() {
  var otherLetters = ['AB', 'MB', 'NB', 'NL', 'YT', 'NS', 'QC'];
  var province = $('#sfstate').val();
  if (province == 'BC') {
    $("#retURL").val("https://test.cleardent.com/demo-thankyou-bc.html");
  } else {
    if (province == 'ON') {
      $("#retURL").val("https://test.cleardent.com/demo-thankyou-on.html");
    } else {
      if (otherLetters.indexOf(province) > -1) {
        $("#retURL").val("https://test.cleardent.com/demo-thankyou-other.html");
      } else {
        $("#retURL").val("https://test.cleardent.com/demo-thankyou-na.html");
      }
    }
  }
}

关于javascript - 如何使用 "JavaScript Switch Statement"重定向用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48509229/

相关文章:

javascript - 按正则表达式拆分产量 "undefined"

javascript - 为动态创建的列表项及其内容添加单击操作

javascript - 如何使用 mongoose 保存嵌套的 mongoDB 属性?

javascript - JSON 到 HTML 表格代码

javascript - 如何在 html 页面之间设置自定义预加载器?

html - 当 flexbox 中只有一个元素时居中

javascript - 在正确的上下文中捕获异步异常

javascript - jQuery removeProp 不起作用

jquery - “SQLSTATE[23000] : Integrity constraint violation: 1062 Duplicate entry

javascript - 使用Jquery动态添加不同形式的字段