简体   繁体   English

我有一个选项表单,我想为我选择的选项标签的每个元素显示来自 JSON 文件的不同图像

[英]I have an option form and I would like to show a different image from a JSON file for each element of the option tag i choose

so I created an option form and at the bottom of this I have a div where i want to show images taken from a JSON file based on which voice of the option form i select.所以我创建了一个选项表单,在这个底部我有一个 div,我想根据我选择的选项表单的声音显示从 JSON 文件中获取的图像。 To be more specific, I'll write some code down here:更具体地说,我将在这里写一些代码:

HTML: HTML:

<select name="classes">
  <option value="knight">Knight</option>
  <option value="mage">Mage</option>
  <option value="priest">Priest</option>
</select>

JS: JS:

const classes = [
    {
        id: 1,
        class: "knight",
        male: "../class-png/male-knight.png",
        female:"../class-png/female-knight.png"
    },
    {
        id:2,
        class:"mage",
        male:"../class-png/male-mage.png",
        female:"../class-png/female-mage.png"
    },
    {
        id: 3,
        class: "priest",
        male: "../class-png/male-priest.png",
        female:"../class-png/female-priest.png",
    }
];

So if i select the "knight" option, i want to show the two png of the knights inside a div below and so on for the mage.因此,如果我选择“骑士”选项,我想在下面的 div 中显示骑士的两个 png,依此类推,以供法师使用。 . . . .
I would to do this using the id of the JSON objects我会使用 JSON 对象的 id 来做到这一点

Your html will be like this你的 html 会是这样

<select class="classes" name="classes">
   <option value="knight">Knight</option>
   <option value="mage">Mage</option>
   <option value="priest">Priest</option>
</select>
<div id="imageWrapper"></div>

Here is your jquery code based on your selected option value这是基于您选择的选项值的 jquery 代码

<script>
    let selectedClass = "knight";
    const classes = [
    {
        id: 1,
        class: "knight",
        male: "../class-png/male-knight.png",
        female:"../class-png/female-knight.png"
    },
    {
        id:2,
        class:"mage",
        male:"../class-png/male-mage.png",
        female:"../class-png/female-mage.png"
    },
    {
        id: 3,
        class: "priest",
        male: "../class-png/male-priest.png",
        female:"../class-png/female-priest.png",
    }
];
// To load Images for pre-selected option
addImage();
$(document).ready(() => {
    $(".classes").change(() => {
        $("#imageWrapper").empty();
        selectedClass = $('.classes :selected').val();
        // change image on option change
        addImage();
    })
})
function addImage (){
    $.each(classes, function( index, value ) {
        if(value.class == selectedClass){
            $("#imageWrapper").prepend('<img id="image" src="'+value.male+'" /><img id="image" src="'+value.female+'" />')
        }
    });
}
</script>

暂无
暂无

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

相关问题 对于一系列图片,我希望每个图像都有不同的音频 - With an array of pictures I would like to have a different audio for each image 如何根据我从选择组合中选择的选项显示不同的窗口? - how can i show a different window depending on the option i choose from a selection combo? 我有JSON数据,我想在Full Calender上显示 - I have JSON data and I would like to show that on Full Calender 我需要在选项标签中切换选择元素 - I need to toggle a select element in option tag 我如何在反应中进行验证,如果我从下拉菜单中选择了一个选项,那么该选项将在另一个下拉菜单中被禁用 - How can I put validation in react, that if I have choose an option from a drop down, then that option will be disable in the other drop down menu 我该如何选择<option>两次<select>元素 (API) - How do I choose an <option> twice on a <select> element (API) 如何解析jQuery中的JSON数组,可视化页面上的每个元素,并对每个要分割的元素进行选择? - How can I parse a JSON array in jQuery, visualize each element on a page with an option on each element to be sected? 我想按事件显示 HTML 元素 - I would like to show HTML element by event 我如何有一个form :: select运行脚本来更改目标,以便每次选择不同的选项时样式都会更改? - How do I have a form::select run a script to change the target so the style changes each time a different option is selected? 读取本地文件,编码为base64,我想给用户一个选项,将结果保存到文件 - Reading a local file, encoding to base64, I would like to give the user an option to save the result to file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM