简体   繁体   English

Javascript:根据用户输入显示随机图像

[英]Javascript: displaying random images based on user input

So I am creating a workout website as a hobby and need some help on the workout creator aspect. 所以我创建一个锻炼网站作为一种爱好,需要一些关于锻炼创造者方面的帮助。 So I have a page where I want to randomly create a workout based on the restrictions a person has. 所以我有一个页面,我想根据一个人的限制随机创建一个锻炼。 Here is the webpage for that part: random workout creator . 以下是该部分的网页: 随机锻炼创建者 Basically, after a person chooses their restrictions for the workout, they will click the "create workout" button, and then the page will display 10 different pictures of exercises for the user to complete. 基本上,在一个人选择他们对锻炼的限制之后,他们将点击“创建锻炼”按钮,然后页面将显示10个不同的锻炼图片供用户完成。 So here is my problem: When I put "No restrictions" and create the workout, only one image shows up, not 10. And when I put either "pregnant" or "low impact" click the "create workout" button, nothing happens. 所以这是我的问题:当我选择“无限制”并创建锻炼时,只显示一张图像,而不是10张。当我选择“怀孕”或“低影响”时,点击“创建锻炼”按钮,没有任何反应。 Can anyone look at my javascript and see what I am doing wrong? 任何人都可以看看我的JavaScript,看看我做错了什么? Here is the code for the javascript: 这是javascript的代码:

function createOffice(){
var randomimages=new Array();

randomimages[0]="images/office/arm circles.gif"
randomimages[1]="images/office/calf raises.gif"
randomimages[2]="images/office/chair step ups.gif"
randomimages[3]="images/office/curtsy lunge.gif"
randomimages[4]="images/office/desk pushups.gif"
randomimages[5]="images/office/lunge2.gif"
randomimages[6]="images/office/Lunges.gif"
randomimages[7]="images/office/side lunge.gif"
randomimages[8]="images/office/Squat.gif"
randomimages[9]="images/office/squat punches.gif"
randomimages[10]="images/office/Step up with knee up.gif"
randomimages[11]="images/office/tricep dips.gif"
randomimages[12]="images/office/Butt Kickers.gif"
randomimages[13]="images/office/high knees.gif"
randomimages[14]="images/office/jogging in place.gif"
randomimages[15]="images/office/jumping butt kicks.gif"
randomimages[16]="images/office/jumping jacks.gif"
randomimages[17]="images/office/jumping rope.gif"
randomimages[18]="images/office/Star Jumps.gif"
randomimages[19]="images/office/wall jump touch.gif"

var preload=new Array()

for (n=0;n<randomimages.length;n++){
    preload[n]=new Image()
    preload[n].src=randomimages[n]
}

if(document.getElementById("impact").checked == false && document.getElementById("pregnant").checked == false && document.getElementById("none").checked == false){
    document.getElementById("error").innerHTML = "*All fields are required";
}
else{
    if(document.getElementById("pregnant").checked == true || document.getElementById("impact").checked == true){
        document.getElementById("img1").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img2").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img3").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img4").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img5").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img6").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img7").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img8").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img9").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img10").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')       
    }
    else{
        document.getElementById("img1").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img2").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img3").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img4").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img5").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img6").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img7").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img8").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img9").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img10").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')       
    }
}
}

First you have to create the img divs you are trying to modify: 首先,您必须创建要修改的img divs

<div id="img1"></div>
<div id="img2"></div>
<div id="img3"></div>
<div id="img4"></div>
<div id="img5"></div>
<div id="img6"></div>
<div id="img7"></div>
<div id="img8"></div>
<div id="img9"></div>
<div id="img10"></div>

Then, replace each of these lines 然后,替换这些行中的每一行

document.getElementById("img1").innerHTML = document.write(<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">)

with

document.getElementById("img1").innerHTML = '<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">' 

Keep in mind that document.write does not return anything, so you cannot set it to your innerHTML . 请记住, document.write不会返回任何内容,因此您无法将其设置为innerHTML

There are quite a few things wrong with your code. 你的代码有很多问题。 like eventHandler said, you should have semicolons, and like rla4 said you need to remove the document.write function. 就像eventHandler所说,你应该有分号,并且像rla4一样说你需要删除document.write函数。

I suggest an entirely different implentation however. 但我建议采用完全不同的实施方式。

Your implementation will reuse images, which I don't think you want. 您的实现将重用图像,这是我认为您不想要的。

Here is a function that won't reuse images: 这是一个不会重用图像的函数:

function randoms(many) {
    var arr = randomImages,
        i;
    while (many < (arr.length)) {
        arr.splice(Math.floor(Math.random() * (arr.length - 1)), 1);
    }
    return arr;
}

Then you can add it to your page: 然后您可以将其添加到您的页面:

randoms(10).forEach(function (img) {
    var elem = document.createElement('img');
    elem.src = img;
    document.getElementById('images').appendChild(elem);
});

Here it is in action: JSFiddle for Random Images 在这里它是行动: JSFiddle随机图像

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM