简体   繁体   English

在Java脚本中,如何将这些数组与该用户输入链接起来,以便将食物和水果打印出来

[英]In java script how do I link these arrays with this user input so it will print out the food and the fruit

I am trying to get the user to put in a number press the submit button and then it outputs the food and the fruit together so that it will be like a lunch thing. 我试图让用户输入数字,然后按“提交”按钮,然后将食物和水果一起输出,这样就好像午餐一样。 PLease help. 请帮忙。 Thanks 谢谢

<!DOCTYPE html>
<html>
<body>

<form action="demo_form.asp">
  Type of food (between 0 and 5): <input type="number" name="x" min="0" max="5">
  </form>

<script>
var food = new Array();
food[0] = "hot dog";
food[1] = "chicken fingers";
food[2] = "veggie sandwhich";
food[3] = "Home fries"
food[4] = "pb and j"
food[5] = "pizza"

var y = Math.floor(Math.random()*6)
var fruit = new Array();
fruit[0] = "orange";
fruit[1] = "banana";
fruit[2] = "apple";
fruit[3] = "kiwi"
fruit[4] = "lemon"
fruit[5] = "peach"

{
document.write(food[x] + fruit[y]+"<br>");
}
</script>


</body>
</html>

You need to add an onchange handler to the input element or add a button with an onclick handler to trigger the action after the user enters a number. 您需要在输入元素上添加onchange处理程序,或者添加带有onclick处理程序的按钮以在用户输入数字后触发操作。 That should call a function to display the values, probably in another HTML element. 那应该调用一个函数来显示值,可能在另一个HTML元素中。 You can use addEventListener or simply add the call to the element something like this: 您可以使用addEventListener或将调用添加到元素中,如下所示:

<input type="number" name="x" min="0" max="5" onchange="showLunch(this);" />  // (note the closed input element!)

The onchange will fire when the user tabs out of the input field. 当用户跳出输入字段时,将触发onchange。 You may want to try a button with onclick or whatever. 您可能要尝试使用onclick或其他任何按钮。 Experiment. 实验。

Your form is not going to be posted to a server, so there's really no need for it. 您的表单不会被发布到服务器上,因此实际上不需要它。 Everything will happen in the Javascript code. 一切都会在Javascript代码中发生。 Your function could be something like: 您的功能可能类似于:

function showLunch( btn ) {
   document.getElementById('lunch').innerHTML = "lunch is " + food(btn.value) + fruit[btn.value];
}

In the function I assume an added HTML div to hold the answer: 在函数中,我假设添加了HTML div以保存答案:

<div id="lunch" ></div>

This is just a start, you may need to clean it up as you go. 这只是一个开始,您可能需要在旅途中对其进行清理。 Have fun... 玩得开心...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java Script从输入区域获取用户输入并分配给变量? - How do I get user input from input area with Java Script and assign to a variable? 我们如何在 java 脚本控制台中获取用户输入 - How do we get user input in java script console 如何为该Java脚本单选添加第二个链接? - How do I add a second link to this java script radio selection? 如何从指向Java脚本数组的链接存储JSON? - How do i store JSON from a link to java script array? 如何在Java脚本中添加“ nofollow”链接 - How do I add 'nofollow' link in java script 如何将阵列打印到控制台? - How do I print the arrays to the console? 如何使用javascript从多个文本输入中打印数组列表? - How do I print the a list of arrays from multiple text input using javascript? 如何在java查询的循环中打印“resultList”并使用javascript打印出来? - how do i print a “resultList” in a loop from java query and also print it out using javascript? 需要将用户输入从提示框(Javascript)放入String变量。 我该怎么做? - Need to place user input from a prompt box (Javascript) into a String variable. How do I do so? 如何通过将链接与用户输入文本进行匹配来让 Python 和 Selenium 单击 angularjs 链接? - How do I get Python and Selenium to click an angularjs link by matching the link to user input text?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM