简体   繁体   English

嵌套的div显示和隐藏下拉列表值选择

[英]nested div display and hide on dropdown value selection

I am trying to open drop down and other fields on the basis of others. 我正在尝试在其他基础上打开下拉列表和其他字段。 I am trying to create a form for clients to fill their form with specific requirement. 我正在尝试为客户创建一个表格,以使其满足特定要求。

The DIV will hide and show on the basis of dropdown selected using jquery. DIV将根据使用jquery选择的下拉菜单隐藏和显示。

Here is how my html code looks like. 这是我的html代码的样子。

<div class="website-type">
                <label class=""><b>Website Type</b>
                    <select>
                        <option>--------Select--------</option>
                        <option value="static-website">Static Website</option>
                        <option value="dynamic-website">Dynamic Website</option>
                        <option value="ecommerce-website">eCommerce Website</option>
                        <option value="seo">Search Engine Optimization</option>
                        <option value="graphic-design">Graphic Design</option>
                    </select>
                </label>
            </div>


            <div class="static-website" id="static-website">

                    <label class=""><b>Design Level</b>
                        <select>
                                <option>--------Select--------</option>
                                <option value="basic-design">Basic Design</option>
                                <option value="business-design">Business Design</option>
                                <option value="creative-design">Creative Design</option>
                        </select>
                    </label>
                     <br/>



                <div class="static-website-basic">

                    <label class="no-pages-static"><b>Number Of Pages</b>
                        <input type="text" name="no-pages-static" value="5" />
                    </label>

                     <br/>


                    <label class="no-form-static"><b>Number Of Simple Form</b>
                        <input type="text" name="no-form-static" value="5" />
                    </label>

                     <br/>

                    <label class="seo-static"><b>SEO (On Page)</b>
                        <input type="radio" name="seo-static" group="seo-static"> Yes
                        <input type="radio" name="seo-static" group="seo-static"> No

                    </label>

                    <br/>

                    <label class="content-writting-static"><b>Content Writing</b>
                        <input type="radio" name="content-static" group="content-writting-static"> Yes
                        <input type="radio" name="content-static" group="content-writting-static"> No
                    </label>
                 </div><!-- End of Basic Static Box -->



                 <div class="static-website-business">



                    <label class="no-pages-static"><b>Number Of Pages</b>
                        <input type="text" name="no-pages-static" value="10" />
                    </label>

                     <br/>


                    <label class="no-form-static"><b>Number Of Simple Form</b>
                        <input type="text" name="no-form-static" value="10" />
                    </label>

                     <br/>

                    <label class="seo-static"><b>SEO (On Page)</b>
                        <input type="radio" name="seo-static" group="seo-static"> Yes
                        <input type="radio" name="seo-static" group="seo-static"> No

                    </label>

                    <br/>

                    <label class="content-writting-static"><b>Content Writing</b>
                        <input type="radio" name="content-static" group="content-writting-static"> Yes
                        <input type="radio" name="content-static" group="content-writting-static"> No
                    </label>
                 </div><!-- End of BUSINESS Static Box -->



                 <div class="static-website-creative">



                    <label class="no-pages-static"><b>Number Of Pages</b>
                        <input type="text" name="no-pages-static" value="5" />
                    </label>

                     <br/>


                    <label class="no-form-static"><b>Number Of Simple Form</b>
                        <input type="text" name="no-form-static" value="5" />
                    </label>

                     <br/>

                    <label class="seo-static"><b>SEO (On Page)</b>
                        <input type="radio" name="seo-static" group="seo-static"> Yes
                        <input type="radio" name="seo-static" group="seo-static"> No

                    </label>

                    <br/>

                    <label class="content-writting-static"><b>Content Writing</b>
                        <input type="radio" name="content-static" group="content-writting-static"> Yes
                        <input type="radio" name="content-static" group="content-writting-static"> No
                    </label>
                 </div><!-- End of Creative Static Box -->


            </div> <!-- End of Static Box -->

This is just a regular jquery i am using. 这只是我正在使用的常规jquery。 Here is how i am using my jquery 这是我使用我的jQuery的方式

$("select").change(function()
        {
            $( "select option:selected").each(function()
            {
                if($(this).attr("value")=="static-website"){
                    $(".dynamic-website").hide();
                    $(".ecommerce-website").hide();
                    $(".seo").hide();
                    $(".graphic-design").hide();
                    $(".static-website").show();
                }
                if($(this).attr("value")=="basic-design"){
                    $(".static-website-business")..hide();
                    $(".static-website-creative").hide();


                    $(".static-website-basic").show();
                }
                if($(this).attr("value")=="business-design"){
                     $(".static-website-basic").hide();
                    $(".static-website-creative").hide();
                    $(".static-website-business").show();
                }
                if($(this).attr("value")=="creative-design"){
                     $(".static-website-basic").hide();
                    $(".static-website-business").hide();
                    $(".static-website-creative").show();

                    ​
                } 

}

});
        }).change();
    });

I dont want so many .hide and .show property for every div. 我不希望每个div有太多.hide和.show属性。 My one drop down is based on another and after selection of 2nd dropdown selection. 我的一个下拉列表基于另一个下拉列表,并且在选择了第二个下拉列表之后。

this is just of Static Website. 这只是静态网站。 Then i have several other fields. 然后我还有其他几个领域。 I dont know how can i do this. 我不知道该怎么办。 I dont want to make it very complicated i just want to make things simple and reusable so i dont have to use so many .hide and .show 我不想使其变得非常复杂,我只想使事情简单易用,因此我不必使用太多.hide和.show

If any suggestions. 如果有任何建议。 please help. 请帮忙。

Thank you! 谢谢!

better hiding and showing as required. 根据需要更好地隐藏和显示。

 var oldselected; $("select").on('change',this,function(e){ if(oldselected){$('.'+oldselected).hide();} var selectedopt=$(this).val(); $('.'+selectedopt).show(); oldselected=selectedopt; }); 
 div:not(.website-type){display:none;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="website-type"> <label class=""><b>Website Type</b> <select> <option>--------Select--------</option> <option value="static-website">Static Website</option> <option value="dynamic-website">Dynamic Website</option> <option value="ecommerce-website">eCommerce Website</option> <option value="seo">Search Engine Optimization</option> <option value="graphic-design">Graphic Design</option> </select> </label> </div> <div class="static-website" id="static-website"> <label class=""><b>Design Level</b> <select> <option>--------Select--------</option> <option value="basic-design">Basic Design</option> <option value="business-design">Business Design</option> <option value="creative-design">Creative Design</option> </select> </label> <br/> <div class="static-website-basic"> <label class="no-pages-static"><b>Number Of Pages</b> <input type="text" name="no-pages-static" value="5" /> </label> <br/> <label class="no-form-static"><b>Number Of Simple Form</b> <input type="text" name="no-form-static" value="5" /> </label> <br/> <label class="seo-static"><b>SEO (On Page)</b> <input type="radio" name="seo-static" group="seo-static"> Yes <input type="radio" name="seo-static" group="seo-static"> No </label> <br/> <label class="content-writting-static"><b>Content Writing</b> <input type="radio" name="content-static" group="content-writting-static"> Yes <input type="radio" name="content-static" group="content-writting-static"> No </label> </div><!-- End of Basic Static Box --> <div class="static-website-business"> <label class="no-pages-static"><b>Number Of Pages</b> <input type="text" name="no-pages-static" value="10" /> </label> <br/> <label class="no-form-static"><b>Number Of Simple Form</b> <input type="text" name="no-form-static" value="10" /> </label> <br/> <label class="seo-static"><b>SEO (On Page)</b> <input type="radio" name="seo-static" group="seo-static"> Yes <input type="radio" name="seo-static" group="seo-static"> No </label> <br/> <label class="content-writting-static"><b>Content Writing</b> <input type="radio" name="content-static" group="content-writting-static"> Yes <input type="radio" name="content-static" group="content-writting-static"> No </label> </div><!-- End of BUSINESS Static Box --> <div class="static-website-creative"> <label class="no-pages-static"><b>Number Of Pages</b> <input type="text" name="no-pages-static" value="5" /> </label> <br/> <label class="no-form-static"><b>Number Of Simple Form</b> <input type="text" name="no-form-static" value="5" /> </label> <br/> <label class="seo-static"><b>SEO (On Page)</b> <input type="radio" name="seo-static" group="seo-static"> Yes <input type="radio" name="seo-static" group="seo-static"> No </label> <br/> <label class="content-writting-static"><b>Content Writing</b> <input type="radio" name="content-static" group="content-writting-static"> Yes <input type="radio" name="content-static" group="content-writting-static"> No </label> </div><!-- End of Creative Static Box --> </div> <!-- End of Static Box --> 

I built a quick utility to do this, it accepts a few data attributes... 我构建了一个快速实用程序来执行此操作,它接受一些数据属性...

http://jsfiddle.net/buxbajvd/ http://jsfiddle.net/buxbajvd/

<form class="progressiveDisclosure">
    <select name="selectNode" class="associatedHandler">
        <option>foobar</option>
        <option>something</option>
    </select>
</form>

<div class="associatedListener" style="display: none;" data-for="selectNode" data-value="something" data-props="matchAll">selectNode is something</div>

The data-for must match the handlers name attribute and the data-value must match its value for this node to show. data-for必须匹配处理程序名称属性,而data-value必须匹配其值,此节点才能显示。 It can also accept multiple arguments data-for="someSelect,someSelect2" data-value="someValue,someValue2" 它还可以接受多个参数data-for="someSelect,someSelect2" data-value="someValue,someValue2"

It is also possible to use reverse conditioning and setting data-value="!someValue" 也可以使用反向条件并设置data-value="!someValue"

var progressiveDisclosure = $('.progressiveDisclosure'),
    associatedHandlers = $('.associatedHandler'),
    associatedListeners = $('.associatedListener'),

    toggleAssociatedListeners = function(e){
        var name = $(e.target).attr('name');

        $.each(associatedListeners, function(index, node){

            var names = node.associationFor,
                values = node.associationValue,
                valid = false,
                matchAll = false;

            if(node.associationOpts && node.associationOpts.indexOf('matchAll') > -1){
                matchAll = true;
            }

            var inputIsAssociation = false;
            for(var i=0;i<names.length;i++){

                if(names[i] == name){
                    inputIsAssociation = true;
                }

                var value = progressiveDisclosure.serializeArray().filter(function(input){
                    return input.name == names[i];
                })[0].value;

                if(values[i].indexOf('!') > -1 && values[i].replace('!','') !== value){
                    valid = true;
                } else if(values[i].indexOf('!') > -1 && values[i].replace('!','')  == value){
                    valid = false;
                    if(!matchAll){
                        break;
                    }
                } else if(values[i] == value){
                    valid = true;
                    if(!matchAll){
                        break;
                    }
                } else if(matchAll){
                     valid = false;
                    break;
                }
            }

            if(!inputIsAssociation){
                return;
            }

            if(valid){
                $(node).show();
            } else if(inputIsAssociation) {
                $(node).hide();
            }

        });

    }

$.each(associatedListeners, function(index, node){
    var $node = $(node),
        opts = $node.data('props');
    node.associationFor = $node.data('for').split(',');
    node.associationValue = $node.data('value').split(',');
    if(opts){
        node.associationOpts = opts.split(',');
    }
});

associatedHandlers.on('change', this, function(e){
    toggleAssociatedListeners(e);
});

I would go with the hide all show what you want but with a plus try making up names from the original selectors therefore: 我会全部隐藏显示您想要的内容,但是还要尝试从原始选择器中组成名称,因此:

$('select').change(){
     $('.divclass').hide();//which youll have to add
     var id = $(this).val(); //or 
              $(this).find('selected').val() 
    //not sure which works for selects
     id=id.replace('design','')
     //obviously you need a replace for this specific example('design','');
     $('#static-website-'+id).show();
}

In a few words the ids you want to show and the options or values ae related to the ids 简而言之,您要显示的ID和与ID相关的选项或值AE

You can hide all , and show what you want. 您可以隐藏全部,并显示您想要的内容。

    function showD(whatYouWantShow){

    //hide all  $(".mydiv").hide();
    //show whatYouWantShow   $("#d1").show();
    }

<div class=mydiv id="d1"></div>
<div class=mydiv id="d2"></div>
<div class=mydiv id="d3"></div>

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

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