switch 语句在 JavaScript 中不起作用

我遇到一个问题,其中一个 javascript 函数正在获取一个selectedMonth字符串 var,并且警报对话框显示该字符串的值,但由于某种原因 switch 语句不起作用。我在页面加载时遇到这个问题,它确实可以与<select>onchange 监听器一起使用。


在这种情况下,我收到警报Dezember,但 switch 语句 december 没有被调用。


function showDropDown(selectedMonth) {


  alert(selectedMonth);


  var settings = {

    "url": "",

    "method": "GET",

    "timeout": 0,

  };


  switch (selectedMonth) {

    case 'Januar':

      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/1"

      break;

    case 'Februar':

      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/2"

      break;

    case "März":

      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/3"

      break;

    case "April":

      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/4"

      break;

    case "Mai":

      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/5"

      break;

    case "Juni":

      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/6"

      break;

    case "Juli":

      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/7"

      break;

    case "August":

      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/8"

      break;

    case "September":

      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/9"

      break;

    case "Oktober":

      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/10"

      break;

    case 'November':

      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/11"

      break;

    case 'Dezember':

      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/12"

      break;

    default:

      // code block

  }


  alert(settings.url);

}


    $(".New_Plant_Month").change(function () {


        // Fetching Value

        var month = $(this).val();

        showDropDown(month);


    });



蝴蝶不菲
浏览 154回答 4
4回答

慕标5832272

我想你需要的是一个事件监听器,当用户更改下拉列表时,你将调用不同的 api。这里我添加了事件监听器,当下拉菜单发生变化时,它会调用你的 showDropdown 函数。$('.New_Plant_Month').change((e) => {&nbsp;showDropDown(e.target.value);})这是代码function showDropDown(selectedMonth) {console.log(selectedMonth);&nbsp; var settings = {&nbsp; &nbsp; "url": "",&nbsp; &nbsp; "method": "GET",&nbsp; &nbsp; "timeout": 0,&nbsp; };&nbsp; switch (selectedMonth) {&nbsp; &nbsp; case 'Januar':&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/1"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case 'Februar':&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/2"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "März":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/3"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "April":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/4"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "Mai":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/5"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "Juni":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/6"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "Juli":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/7"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "August":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/8"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "September":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/9"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "Oktober":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/10"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case 'November':&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/11"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case 'Dezember':&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/12"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; // code block&nbsp; }&nbsp; alert(settings.url);}// show months$(document).ready(function() {&nbsp; for (var i = 0; i < 6; i++) {&nbsp; &nbsp; var d = new Date();&nbsp; &nbsp; var months = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"];&nbsp; &nbsp; var monthName = months[new Date(d.setMonth(d.getMonth() - i)).getMonth()];&nbsp; &nbsp; //console.log(monthName);&nbsp; &nbsp; $('.New_Plant_Month').append($("<option>").val(monthName).text(monthName));}&nbsp; var month = $(".New_Plant_Month&nbsp; option:selected").text() + "";showDropDown(month);&nbsp; $('.New_Plant_Month').change((e) => {&nbsp; &nbsp; &nbsp;showDropDown(e.target.value);&nbsp; })});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><select class="New_Plant_Month"></select>

白衣染霜花

<select>我发现的唯一解决办法是在页面加载时通过 jquerychange侦听器以编程方式触发,否则永远var selectedMonth不会switch通过..如上面的 gif 屏幕截图所示。无论如何,这是解决办法。&nbsp; &nbsp; &nbsp; &nbsp; $(document).ready(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.New_Plant_Month')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .trigger('change');&nbsp; &nbsp; &nbsp; &nbsp; });

扬帆大鱼

请尝试此代码,切换语句在 javascript 中不起作用<html>&nbsp; &nbsp;<body>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; <script type = "text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<!--&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var grade = 'C';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.write("Entering switch block<br />");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (grade) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'A': document.write("Good job<br />");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'B': document.write("Pretty good<br />");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'C': document.write("Passed<br />");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'D': document.write("Not so good<br />");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 'F': document.write("Failed<br />");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default:&nbsp; document.write("Unknown grade<br />")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.write("Exiting switch block");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//-->&nbsp; &nbsp; &nbsp; </script>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; <p>Set the variable to different value and then try...</p>&nbsp; &nbsp;</body></html>我希望这段代码对您有用。

慕勒3428872

代码的顺序必须有点不同 - 就像这样:function showDropDown(selectedMonth) {&nbsp; alert(selectedMonth);&nbsp; var settings = {&nbsp; &nbsp; "url": "",&nbsp; &nbsp; "method": "GET",&nbsp; &nbsp; "timeout": 0,&nbsp; };&nbsp; switch (selectedMonth) {&nbsp; &nbsp; case 'Januar':&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/1"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case 'Februar':&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/2"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "März":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/3"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "April":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/4"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "Mai":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/5"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "Juni":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/6"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "Juli":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/7"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "August":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/8"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "September":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/9"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case "Oktober":&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/10"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case 'November':&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/11"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case 'Dezember':&nbsp; &nbsp; &nbsp; settings.url = "https://sslbeta.de/api/plantsearchapi/latest/12"&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; // code block&nbsp; }&nbsp; alert(settings.url);}// show months$(document).ready(function() {&nbsp; for (var i = 0; i < 6; i++) {&nbsp; &nbsp; var d = new Date();&nbsp; &nbsp; var months = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"];&nbsp; &nbsp; var monthName = months[new Date(d.setMonth(d.getMonth() - i)).getMonth()];&nbsp; &nbsp; //console.log(monthName);&nbsp; &nbsp; $('.New_Plant_Month').append($("<option>").val(monthName).text(monthName));&nbsp; }var month = $(".New_Plant_Month&nbsp; option:selected").text() + "";&nbsp; showDropDown(month);});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><select class="New_Plant_Month"></select>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript