Drop down list - display div when clicking an option

前端 未结 2 1337
小鲜肉
小鲜肉 2020-12-21 11:47

You must write a function that \'shows\' the corresponding part of the form depending on what the user has selected. For example, if \'Pizza\' is selected, the div #section2

2条回答
  •  [愿得一人]
    2020-12-21 12:12

    There are plenty of solutions for that. One of the simpliest options is to change your markup a bit and use div IDs.

    HTML:

    JavaScript:

    function selection(select) {
        document.getElementById("section_" + select.value).style.display = "block";
    }
    

    However, you can use JQuery library and make it faster and more flexible. You can easily add animation to blocks and add extra functionality.

    HTML:

    JavaScript:

    $("#food").change(function() {
        $("[data-type]").hide();
        $("[data-type='" + this.value + "']").show(1000);
    });
    

提交回复
热议问题