【问题标题】:combining switch loops to concatenate the result组合开关循环以连接结果
【发布时间】:2019-12-24 10:05:54
【问题描述】:

请原谅我的菜鸟。

所以,我想出了如何创建两个单独的开关循环,以便访问两个单独的值。我现在的麻烦是我不知道如何编写一个函数来连接两个开关值的结果。

var select = document.getElementById('weather');
var para = document.getElementById('alf');
var door = document.getElementById('dance');
var mort = document.getElementById('tim');
document.getElementById('weather').addEventListener('change', 
setWeather);
document.getElementById('dance').addEventListener('change', 
setDance);



function setWeather() {
  var choice = this.value;

  switch(choice) {
   case 'sunny':
     para.textContent = "fly me to the Moon";
     break;
   case 'rainy':
     para.textContent = "let me sail amongst the stars";
     break;
   case 'snowy':
     para.textContent = "life on Jupiter and Mars";
     break;
default:
  para.textContent = "";
 }
 }
  function setDance() {
  var art = this.value;

switch(art) {
  case 'hail':
    mort.textContent = "in other words";
    break;
  case 'rail':
    mort.textContent = "darling kiss me";
    break;
  case 'cat':
    mort.textContent = "please be true";
    break;
  default:
    mort.textContent = "";
 }
 }

function result() {
  weatherVal = weather.value;
  danceVal = dance.value;
  para.textContent = weatherVal + danceVal;
}

我尝试使用上面的代码“function result()”来编写函数来创建连接,但没有成功。我觉得答案就在我面前,但我不太确定发生了什么。

【问题讨论】:

    标签: arrays function loops if-statement


    【解决方案1】:

    您可以做出选择和艺术全局变量,然后在结果中使用它们,如下所示:

    var select = document.getElementById('weather');
    var para = document.getElementById('alf');
    var door = document.getElementById('dance');
    var mort = document.getElementById('tim');
    document.getElementById('weather').addEventListener('change', setWeather);
    document.getElementById('dance').addEventListener('change', setDance);
    var choice;
    var art;
    
    
    function setWeather() {
      choice = this.value;
    
      switch(choice) {
        case 'sunny':
          para.textContent = "fly me to the Moon";
          break;
        case 'rainy':
          para.textContent = "let me sail amongst the stars";
          break;
        case 'snowy':
          para.textContent = "life on Jupiter and Mars";
          break;
        default:
          para.textContent = "";
          break;
      }
    }
    
    function setDance() {
      art = this.value;
    
      switch(art) {
        case 'hail':
          mort.textContent = "in other words";
          break;
        case 'rail':
          mort.textContent = "darling kiss me";
          break;
        case 'cat':
          mort.textContent = "please be true";
          break;
        default:
          mort.textContent = "";
          break;
      }
    }
    
    function result() {
      para.textContent = choice + art;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 2021-11-06
      • 1970-01-01
      • 2016-09-08
      • 2021-02-24
      相关资源
      最近更新 更多