简体   繁体   中英

Change style of select box when Other select box change in html

I am trying to change the CSS property of select box when other select box is change.

When Id=project-type , is changes its value than bgt-hourly and bgt-fixed select box show show accordingly.

if hourly selected than bgt-hourly should show and when it select Fixed than bgt-fixed select options should show.

i tried many time with different codes from stack-overflow but it didn't help me well.

if any one can solve this than I appreciate his/her help.

thanks

I have following Code:

HTML:

<span class="service-tab">
    <span class="sub-cat span6">
        <label for="project-type">Project Type:</label>
        <select class="project-type  " id="project-type" >
            <option value="hourly">Hourly</option>
            <option value="fixed">Fixed</option>
        </select>
    </span>
    <span class="sub-cat span6 " id="bgt-fixed">
        <label for="budget">Budget:</label>
        <select class="budget  " id="budget" required >
            <option value="250">$0 - $250</option>
            <option value="750">$250 - $750</option>
            <option value="5000">$750 - $5000</option>
            <option value="5000">$1500 - $5000</option>
            <option value="5000">$3000 - $5000</option>
            <option value="10000">$5000 - $10000</option>
            <option value="10001">$10000 and Above</option>
        </select>
    </span>
    <span class="sub-cat span6" id="bgt-hourly">
        <label for="budget-hourly">Budget:</label>
        <select class="budget-hourly" id="budget-hourly" required>
            <option value="10">$0 - $10</option>
            <option value="20">$10 - $20</option>
            <option value="50">$20 - $50</option>
            <option value="100">$50 - $100</option>
            <option value="200">$100 - $200</option>
        </select>
    </span>
</span>

JS:

<script type="text/javascript">
$(document).ready(function(){

    $("#project-type").change(function(){
        if($('#project-type').val() == "fixed")
        {
            $('#bgt-fixed').show();
            alert('value 1 (wich refers to Chef) got selected');
        }
    });
    if($(this).val() == "hourly")
        {
            alert('value 1 (wich refers to Chef) got selected');
        }
    });


    });

Error: you have placed if($(this).val() == "hourly") outside change function.

Try:

$(document).ready(function () {
    $("#project-type").change(function () {
        if ($('#project-type').val() == "fixed") {
            $('#bgt-fixed').show();
            alert('value 1 (wich refers to Chef) got selected');
        } else if ($(this).val() == "hourly") {
            alert('value 1 (wich refers to Chef) got selected');
        }
    });
});

DEMO

must not set class or id same for best code practises

$("#project-type").change(function(){}

this should be

$(".project-type").change(function(){}

and

<select class="project-type" id="projects" >

eliminate extra spaces

$(".project-type").change(function(){...}

Try this:

  $(document).ready(function(){

    $("#project-type").change(function(){
        if($('#project-type').val() == "fixed")
        {
            $('#bgt-fixed').show();
            $('#bgt-hourly').hide();
            $('#project-time').hide();
            $('#project-hours').hide();

        }
        else if($(this).val() == "hourly")
        {
            $('#bgt-hourly').show();
            $('#bgt-fixed').hide();
            $('#project-time').show();
            $('#project-hours').show();

        }
    });    
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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