本文介绍了我可以在一个变量中有多个值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题为单个变量中可以有多个值吗?"

As the title "Can I have multiple values in a single variable?"

首先,我有以下表格:

<form name="myform">
 <input type="text" name="mytext">
 <input type="button" onClick="clickButton()">
</form>

然后,看看我的脚本.

<script>
function clickButton() {
  var x = document.myform.mytext.value;
  var a = 13;
  var b = 17;
  var c = 19;

  if (x == a) {
    alert('hello');
  } else if (x == b) {
    alert('hello');
  } else if (x == c) {
    alert('hello');
  } else {
    alert('goodbye');
  }
}
</script>

有没有办法使一个变量具有多个值?就像var myvalues=1,2,3;

Is there any way to make one variable with multiple values? Like, var myvalues=1,2,3;

推荐答案

对您问题的正确答案是使用数组.但是,从您要尝试执行的操作中,看起来就像您在寻找对象,特别是方括号表示法:

The correct response to your question would be to use an array. But from what you're trying to do, Looks like your looking for an object, specifically the bracket notation:

function clickButton() {
  var x = document.myform.mytext.value,
    greetings = {
      "13": "hello",
      "17": "hello",
      "19": "hello"
    }
  alert(greetings[x] || "goodbye");
}
<form name="myform">
  <input type="text" name="mytext">
  <input type="button" onClick="clickButton()" value="greet">
</form>

这篇关于我可以在一个变量中有多个值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 12:28
查看更多