JavaScript中的switch语句不起作用

时间:2016-11-03 06:35:13

标签: javascript jquery callback switch-statement

在下面的脚本中,我有两个回调函数:getProjectKey()和getWorkflowSchemeName()。

我已传递变量' workflowSchemeName'在switch语句中,虽然我可以在调试器中看到值即将到来,但switch语句不起作用并继续在默认情况下运行。

以下是代码。



if (AJS.$("#issue-create-submit").val() == "Create" || AJS.$("#create-issue-submit").val() == "Create") {
  inputTag = AJS.$("#project")[0];
  projectId = inputTag.value;

  var projectKey;
  var workflowSchemeName;

  function getProjectKey(projectId, callback) {
    console.log("Project ID is (inside function): " + projectId);
    var restCall = AJS.params.baseURL + "/rest/api/2/project/" + projectId;

    AJS.$.get(restCall, function(response) {
      callback(response);
      console.log(" REST PROJECT KEY IS : " + response.key);
    })
  }

  function getWorkflowSchemeName(projectKey, callback) {
    var restCall = AJS.params.baseURL + "/rest/projectconfig/1/workflowscheme/" + projectKey;

    AJS.$.get(restCall, function(response) {
      callback(response);
      console.log(" REST WorkflowScheme Name is: " + response.name);
    })
  }

  if (projectId != null) {
    getProjectKey(projectId, function(response) {
      projectKey = response.key;
      console.log("*************** PROJECT KEY IS : " + projectKey);

      if (projectKey != null) {
        getWorkflowSchemeName(projectKey, function(resp) {
          workflowSchemeName = resp.name;

          // Getting the WorkflowSchemeName till here. CAN see it in the console. 

          console.log("*************** WORKFLOWSCHEME IS : " + workflowSchemeName); // Can see this. 

          if (workflowSchemeName != null) {
            switch (workflowSchemeName) {

              // FAILING HERE
              case "SW Work Flow Scheme":
                console.log("SOFTWARE New Content Inside condition ");
                AJS.$(".field-group").children('label[for=description]').append('<span class="aui-icon icon-required"></span>');
                AJS.$(".field-group").children('label[for=customfield_10105]').append('<span class="aui-icon icon-required"></span>');
                break;

              case "HW Workflow Scheme":
                console.log("HARDWARE New Content Inside condition ");
                AJS.$(".field-group").children('label[for=description]').append('<span class="aui-icon icon-required"></span>');
                break;


                // DEFAULT CASE ALWAYS RUNS. 

              default:
                console.log("A new workflow scheme is detected. Need to update the Scripts Plugin in Create Issue Screens");
            }
          }
        })
      }
    })
  }

  // Can't see these values here either. But separate Issue. 
  console.log("Project Key is ************** " + projectKey);
  console.log("WorkflowScheme Name is ******** " + workflowSchemeName);
}
&#13;
&#13;
&#13;

任何人都可以帮助我,我在这里做错了什么? 非常感谢。

1 个答案:

答案 0 :(得分:1)

switch语句是正确的。问题是var workflowSchemeName的值不是你在switch case中检查的(空格和case重要)。检查一下。

    var workflowSchemeName = 'SW Work Flow Scheme';

    switch (workflowSchemeName) {

        // FAILING HERE
    case "SW Work Flow Scheme":
        console.log("SOFTWARE New Content Inside condition ");
        //AJS.$(".field-group").children('label[for=description]').append('<span class="aui-icon icon-required"></span>');
        //AJS.$(".field-group").children('label[for=customfield_10105]').append('<span class="aui-icon icon-required"></span>');
        break;

    case "HW Workflow Scheme":
        console.log("HARDWARE New Content Inside condition ");
        //AJS.$(".field-group").children('label[for=description]').append('<span class="aui-icon icon-required"></span>');
        break;

        // DEFAULT CASE ALWAYS RUNS.

    default:
        console.log("A new workflow scheme is detected. Need to update the Scripts Plugin in Create Issue Screens");
    }

    var workflowSchemeName = 'HW Workflow Scheme';

    switch (workflowSchemeName) {

        // FAILING HERE
    case "SW Work Flow Scheme":
        console.log("SOFTWARE New Content Inside condition ");
        //AJS.$(".field-group").children('label[for=description]').append('<span class="aui-icon icon-required"></span>');
        //AJS.$(".field-group").children('label[for=customfield_10105]').append('<span class="aui-icon icon-required"></span>');
        break;

    case "HW Workflow Scheme":
        console.log("HARDWARE New Content Inside condition ");
        //AJS.$(".field-group").children('label[for=description]').append('<span class="aui-icon icon-required"></span>');
        break;

        // DEFAULT CASE ALWAYS RUNS.

    default:
        console.log("A new workflow scheme is detected. Need to update the Scripts Plugin in Create Issue Screens");
    }

输出结果为:

"SOFTWARE New Content Inside condition "
"HARDWARE New Content Inside condition "