【问题标题】:Beginner: Extracting multiple variables when prompted初学者:在提示时提取多个变量
【发布时间】:2017-08-10 19:03:10
【问题描述】:

我有以下代码:

我想要做的是当我输入提示时,我希望能够输入多个变量并接收它们的输出。我该怎么做呢。

比如我输入lh0、lh1和lh3,我期望得到000110100110010111101

var outputRef = document.getElementById('outputArea');
var output = " ";

var areas = prompt("Enter no :  "); 

var areas1 = {

        lh0 : "0001101",
        lh1 : "0011001",
        lh2 : "0010011",
        lh3 : "0111101",
        lh4 : "0100011",
        lh5 : "0110001",
        lh6 : "0101111",
        lh7 : "0111011",
        lh8 : "0110111",
        lh9 : "0001011"


};


if (areas1 [ areas ] !== undefined) 
{
    areas = areas1[areas];

}

output = areas;
outputRef.innerHTML = output;

谢谢!

【问题讨论】:

  • 请准确添加您想要输入的内容以获得想要的结果。你有考虑过使用循环吗?
  • “你想输入什么以获得想要的结果”到底是什么意思。我已经说过,使用提示符,我将键入 lh0 等等。我考虑过使用循环,但我不知道如何去做
  • 好吧,你可以写第一个,输入,然后是下一个,tec 或 jusit in one,用空格或逗号分隔,或其他。那么如何将想要的信息输入到单个(?)提示符中?
  • 还有伊丽莎,也刚刚在这里回答了……老实说,你真的想要那个突然出现在你的脸上(提示)吗?你不能使用“html select multiple”或者只是一个普通的“文本框”

标签: javascript string object var


【解决方案1】:

您可以使用Array#split 来拆分用户输入的字符串,例如lh0, lh1, lh2,然后在areas1 对象中找到对应的键并将其值添加到output 字符串中。最后,将其显示在 DOM 中。

var outputRef = document.getElementById('outputArea');
var output = "";

var areas = prompt("Enter no :  ");

var areas1 = {
  lh0: "0001101",
  lh1: "0011001",
  lh2: "0010011",
  lh3: "0111101",
  lh4: "0100011",
  lh5: "0110001",
  lh6: "0101111",
  lh7: "0111011",
  lh8: "0110111",
  lh9: "0001011"
};

areas.split(',').forEach(function(v){
  output += areas1[v.replace(' ', '')];
});

outputRef.innerHTML = output;
<p id="outputArea"></p>

【讨论】:

    【解决方案2】:

    试试这个:

    var outputRef = document.getElementById('outputArea');
    var output = " ";
    
    var areas = prompt("Enter no :  "); 
    
    var areas1 = {
    
            lh0 : "0001101",
            lh1 : "0011001",
            lh2 : "0010011",
            lh3 : "0111101",
            lh4 : "0100011",
            lh5 : "0110001",
            lh6 : "0101111",
            lh7 : "0111011",
            lh8 : "0110111",
            lh9 : "0001011"
    
    
    };
    
    
    var results = [];
    
    if (areas) 
    {
        areas.split(',').forEach(function(val) {
          results.push(areas1[val]);
        });
    }
    
    outputRef.innerHTML = results.join("");
    <div id="outputArea">
    </div>

    【讨论】:

      猜你喜欢
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      • 2023-03-14
      相关资源
      最近更新 更多