如何使用“JavaScript Switch Statement”

时间:2018-01-29 20:11:52

标签: javascript jquery html

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

    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函数添加'Other'并将用户重定向到默认页面,以防万一有人键入随机数,例如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");    
    } 
  }
    }
  }

由于某种原因这不起作用。你能在这里抓住我的错误,大师?

非常感谢, 杰森

1 个答案:

答案 0 :(得分:0)

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");
      }
    }
  }
}