我需要制作简单的javascript代码来显示包含5个元素的水果数组中的一个元素,但条件是水果必须是用户定义的,即水果的索引必须是用户提供的,并使用javascript来打印。
文本区必须是用户输入,并在提交时显示元素
发布于 2015-06-03 18:15:39
有很多方法可以做到这一点,也许最简单的是使用提示框。
var fruits = ["fruit1", "fruit2", "fruit3", "fruit4"];
var choice = prompt("Please enter a number between 0 and " + fruits.length);
// check that it is not null
if(choice)
{
//convert to an integer
parseInt(choice);
alert(fruits[choice]);
//or
console.log(fruits[choice])
}
显然,您可能应该处理字符串的验证,以确保它是一个整数!
https://stackoverflow.com/questions/30617048
复制相似问题