由於標題「我可以在一個變量中有多個值嗎?」我可以在一個變量中有多個值嗎?
首先,我有這種形式:
<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>
有什麼辦法讓一個變量具有多個值嗎? Like,var myvalues=1,2,3;
給回調使用數組? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array – therealrootuser
不,沒有這樣的變量。但是你可以使用'array'甚至'object'。 – sadrzadehsina
另一個選擇是使用字符串,你可以在你的文本框中輸入'「1,2,3」',然後在'if'語句中尋找那些子字符串 –