简体   繁体   English

Javascript,我如何从数组中获取两个相应的元素以使用随机功能同时生成?

[英]Javascript, how do i get two corresponding elements from an array to generate at the same time with the random feature?

i can´t figure out how to get two items from my array list that belong together to generate at the same time when i use the random feature. 当我使用随机功能时,我无法弄清楚如何从数组列表中获得两个在一起生成的项。 When i click on the "get Name and Grade" button i want to get a random name in one box and its corresponding Grade in the other. 当我单击“获取名称和等级”按钮时,我想在一个框中获得一个随机名称,在另一个框中获得其相应的等级。 i can only manage to get random names and grades that does not belong together. 我只能设法获得不在一起的随机名称和等级。 How do i fix this? 我该如何解决?

<!DOCTYPE html>
<html> 

<head> 
<meta charset="UTF-8">
<title>javascript</title>

<script type="text/javascript">


    var names = [];
    var grades = [];

    names[0]="Klara";
    grades[0]="A";
    names[1]="Sarah";
    grades[1]="A";
    names[2]="Andrea";
    grades[2]="B";
    names[3]="Emil";
    grades[3]="C";
    names[4]="Victor";
    grades[4]="C";
    names[5]="Alicia";
    grades[5]="D";
    names[6]="Thomas";
    grades[6]="E";
    names[7]="Robert";
    grades[7]="E";



    function getName()
    {
        var rand = names[Math.floor(Math.random() * 8)];
        return rand;
        var box =document.getElementById('getName');getName.value=getName;

        }

   function box(){
   var box =document.getElementById('getName');getName.value=getName;
   }



            function getGrade()
    {
        var rand = grades[Math.floor(Math.random() * 8)];
        return rand;
        var box =document.getElementById('getGrade');getGrade.value=getGrade;

        }

   function box2(){
   var box2 =document.getElementById('getGrade');getGrade.value=getGrade;
   }






</script>


</head>
<body>

<form>

    <input type="text" name="box" id="box" value=""/> <br/>
    <input type="text" name="box2" id="box2" value=""/> <br/>
    <input type="button" name="textbox1" id="textbox1" value="get Name and Grade" onclick="document.getElementById('box').value = getName(),document.getElementById('box2').value = getGrade()"/> 
    </form>    






</body>
</html>

There are really many syntax errors and weird calls in your example, which shall be ignored for now. 您的示例中确实存在许多语法错误和怪异的调用,现在暂时将其忽略。

You could store the data in objects instead, so the data is always stored together: 您可以将数据存储在对象中,因此数据总是存储在一起:

var tName = [
    {Name: "Klara", Grade: "A"},
    {Name: "Sarah", Grade: "A"}
];

You can then access the values like this: 然后,您可以访问像这样的值:

tName[0].Name;
tName[0].Grade;

Edit: 编辑:

<html> 
    <head> 
        <script>
            var  mName = [
                     {Name: "Klara", Grade: "A"},
                     {Name: "Sarah", Grade: "A"},
                     {Name: "Andrea", Grade: "B"},
                     {Name: "Emil", Grade: "C"},
                     {Name: "Victor", Grade: "C"},
                     {Name: "Alicia", Grade: "D"},
                     {Name: "Thomas", Grade: "E"},
                     {Name: "Robert", Grade: "E"}
            ];

            function getName(){
                return mName[Math.floor(Math.random() * mName.length)]
            }

            function setValues(){
                var tB1 = document.querySelector('#box');
                var tB2 = document.querySelector('#box2');
                var tName = getName();

                tB1 && (tB1.value = tName.Name);
                tB2 && (tB2.value = tName.Grade)
            }
        </script>
    </head>

    <body>
        <form>
            <input type="text" name="box" id="box" value=""/> <br/>
            <input type="text" name="box2" id="box2" value=""/> <br/>
            <input type="button" name="textbox1" id="textbox1" value="get Name and Grade" onclick="setValues()"/> 
        </form>
    </body>
</html>

Okay so now my code looks like this. 好的,现在我的代码看起来像这样。 When i press the button to get grade and name i get [object Object] in each textbox, can´t figure out how to solve it. 当我按下按钮以获取成绩和名称时,我在每个文本框中都获得了[object Object],无法解决该问题。

<!DOCTYPE html>
<html> 

<head> 
<meta charset="UTF-8">
<title>javascript</title>

<script type="text/javascript">


    var  tName = [
         {Name: "Klara", Grade: "A"},
         {Name: "Sarah", Grade: "A"},
         {Name: "Andrea", Grade: "B"},
         {Name: "Emil", Grade: "C"},
         {Name: "Victor", Grade: "C"},
         {Name: "Alicia", Grade: "D"},
         {Name: "Thomas", Grade: "E"},
         {Name: "Robert", Grade: "E"}
    ];

    tName[0].Name;
    tName[0].Grade;
    tName[1].Name;
    tName[1].Grade;
    tName[2].Name;
    tName[2].Grade;
    tName[3].Name;
    tName[3].Grade;
    tName[4].Name;
    tName[4].Grade;
    tName[5].Name;
    tName[5].Grade;
    tName[6].Name;
    tName[6].Grade;
    tName[7].Name;
    tName[7].Grade;


    function getName()
    {
        var rand = tName[Math.floor(Math.random() * tName.length)];
        return rand;
        var box =document.getElementById('getName');getName.value=getName;

        }

   function box(){
   var box =document.getElementById('getName');getName.value=getName;
   }



   function box2(){
   var box2 =document.getElementById('getName');getName.value=getName;
   }






</script>


</head>
<body>

<form>

    <input type="text" name="box" id="box" value=""/> <br/>
    <input type="text" name="box2" id="box2" value=""/> <br/>
    <input type="button" name="textbox1" id="textbox1" value="get Name and Grade" onclick="document.getElementById('box').value = getName(),document.getElementById('box2').value = getName()"/> 
    </form>    






</body>
</html>

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

相关问题 如何从 Javascript 中的数组中获取元素的随机组合? - How do i get a random combination of elements from an array in Javascript? 如何从 javascript 和 python 中的数组生成和求和随机数 - How do i generate and sum up random numbers from an array in javascript and python 如何从 JavaScript 中的数字数组生成非重复随机数? - How do I generate a non-repeating random from an array of number in JavaScript? 如何使用Javascript基于另一个数组的相应元素的属性对数组进行排序? - How do I sort an array based on the attribute of corresponding elements of another array using Javascript? 您如何使用JavaScript在一段时间内从数组中获取随机结果? - How do you use javascript to get a random result from an array over a time interval? 如何将数据元素同时推送到两个数组? - How can i push data elements to two array at same time? 从同一数组中获取两个随机词 - Get two random words from the same array 每次页面刷新时,如何将JavaScript文件中的随机图像加载到HTML中? - How do I load a random image from an array in a JavaScript file into HTML every time the page refreshes? 如何生成伪随机数组? - How do I generate pseudo random array? 有没有办法让两个元素从数组中选择随机项目但不会是同一个项目 - Javascript - is there a way to make two elements pick random items from a array but won't be the same item - Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM