尝试使用输入值打印出 Javascript 数组
Trying to make a Javascript Array Print out using Inputted value
我的第一个问题是我是否正确地将 id "textOne" 中的值获取到我的 var a 中?其次,我应该让我的while循环运行"而表示我要打印的索引值的变量"大于或等于最小数组值。我的 While 循环条件是否正确?
我基本上只是希望它根据 1-5 范围内的用户输入打印出数组中正确数量的索引。
提前谢谢。
<input type="button" value="Click" onclick="JobDuties()" />
to see my top
<input type="text" id="textOne" />
job duties here
<p id="answerswer"></p>
<script type="text/javascript">
function JobDuties()
{
var x = "";
var a = document.getElementById('textOne').value;
var b = a - 1;
myduties = new Array();
myduties[0] = "Saab";
myduties[1] = "Volvo";
myduties[2] = "BMW";
myduties[3] = "Toyota";
myduties[4] = "Ford";
}
while (a>=0)
{
x = x + myduties[b];
document.getElementById("answerswer").innerHTML=x;
}
</script>
我假设你想打印出正确的数组值,而不是数组,因为没有多个。 PS>如果你不这么说,人们不喜欢这里的家庭作业问题,因为完全回答对你的学习没有帮助。 我会部分回答。
- 您无需遍历数组即可查找值。
- 您可以更轻松地初始化数组。["萨博"、"沃尔沃"等]
- 您可以引用数组中的一个位置,就像 Saab 一样,因为它是零索引的我的职责[0]
- 如果你要循环,我仍然不明白你为什么要这样做。
代码如下:
var i = 1; //The variable you increment up to the use input variable a
while(i =< myduties.length){
if(i == a){ //if i and a are same then that position
//in the array will have the car type the user requested
myduties[i-1] //and set it to the DOM or document element similar to what you already had
}
i = i + 1; //Increment i towards the one less than the length of the array
}
不完美或检查,但这应该为您提供一些指示。
相关文章:
- 打印javascript控制台的堆栈跟踪
- php:打印javascript/jquery dom就绪
- 如何在html元素中打印javascript变量
- 在htm-php页面中打印Javascript函数
- 使用请求对象中可用的值打印 JavaScript 警报
- 如何使用PHP打印JavaScript
- 如何在我的视图中打印JavaScript代码
- 在自定义控制台中打印javascript错误
- 使用POS打印机打印Javascript收据
- 在移动设备(ios)上打印Javascript
- 如果会话中的属性不为 null 或默认值,则运行/打印 JavaScript Razor ASP.NET MVC
- 在 PHP 中打印 JavaScript 代码不起作用
- 在 php 中打印 javascript 的值
- 打印 Javascript 而不是 ASP(经典)输出
- 如何在 HTML 中打印 javascript 函数的返回值 “添加 {x} +{y}={result}”
- 在 PHP 中打印 JavaScript 代码
- WebClientPrint 用于 Chrome 上的客户端打印 javascript 问题
- 如何打印 JavaScript 非对象的所有函数
- PHP 字符串包含打印 Javascript 时的换行符问题
- 在 Javascript 中打印 Javascript