简体   繁体   中英

How to trigger a button to appear only if multiple options from different drop-down menus have been selected

I'm trying to make sure a "Done" button pops up after options from 3 separate drop-down menus have been selected (regardless of the specific choices). The button should only appear after all 3 drop-down menus have changed (from no selection to any selection).

Here's my HTML code:

{{# sentPt1 }}
    <div class="sent nodisplay">
    <p class="answer-container dropdown-container">
        <p class="question">
        <select id="response-quant" class="option" name="answer">
            <option disabled selected></option>
            <option value={{ QUANT1 }}>{{ QUANT1 }}</option>
            <option value={{ QUANT2 }}>{{ QUANT2 }}</option>
        </select>
        {{ sentPt1 }}
        <select id="response-shape" class="option" name="answer">
            <option disabled selected></option>
            <option value={{ SHAPE1 }}>{{ SHAPE1 }}</option>
            <option value={{ SHAPE2 }}>{{ SHAPE2 }}</option>
            <option value={{ SHAPE3 }}>{{ SHAPE3 }}</option>
        </select>
        {{ sentPt2 }}
        <select id="response-col" class="option" name="answer">
            <option disabled selected></option>
            <option value={{ COL1 }}>{{ COL1 }}</option>
            <option value={{ COL2 }}>{{ COL2 }}</option>
        </select>
        {{ sentPt3 }}
        </p>
        <button id="done" class="nodisplay" onchange="doneChange">Done</button>
    </p>
    </div>
{{/ sentPt1 }}

And here's the JS/ jQuery part (which of course is not really working for the time being):

$('#response-quant, #response-shape, # response-col').change(function() {
    $('#done').removeClass('nodisplay');
});

I would really like to find a jQuery solution which allows me to keep the structure I have so far. I've tried targeting the #id, the .class, and many other different options.

You can Try something like this:

var response_quant = false;
var response_shape = false;
var response_col = false;

$('#response-quant').change(function() {
    response_quant = true;
    if ( response_quant == true && response_shape == true && response_col == true ) {
  $('#done').removeClass('nodisplay');
}
});
$('#response-shape').change(function() {
    response_shape = true;
    if ( response_quant == true && response_shape == true && response_col == true ) {
  $('#done').removeClass('nodisplay');
}
});
$('#response-col').change(function() {
    response_col = true;
    if ( response_quant == true && response_shape == true && response_col == true ) {
  $('#done').removeClass('nodisplay');
}
});

I hope this will help you.

You could add an if-clause with:

if($('#response-quant').selectedIndex != 0 && $('#response-shape').selectedIndex != 0 && $('#response-col').selectedIndex != 0){
    $('#done').removeClass('nodisplay');
}

although I would suggest the .css()-method instead of removing the class

Try this: HTML

<div class="sent nodisplay">
    <p class="answer-container dropdown-container">
    <p class="question">
        <select id="response-quant" class="option" name="answer" onchange="quantChange()">
            <option disabled selected></option>
            <option value={{ QUANT1 }}>{{ QUANT1 }}</option>
            <option value={{ QUANT2 }}>{{ QUANT2 }}</option>
        </select>
        <select id="response-shape" class="option" name="answer" onchange="shapeChange()">
            <option disabled selected></option>
            <option value={{ SHAPE1 }}>{{ SHAPE1 }}</option>
            <option value={{ SHAPE2 }}>{{ SHAPE2 }}</option>
            <option value={{ SHAPE3 }}>{{ SHAPE3 }}</option>
        </select>
        <select id="response-col" class="option" name="answer" onchange="colChange()">
            <option disabled selected></option>
            <option value={{ COL1 }}>{{ COL1 }}</option>
            <option value={{ COL2 }}>{{ COL2 }}</option>
        </select>
    </p>
    <button id="done" onchange="doneChange" hidden>Done</button>
    </p>
</div>

JS:

var quant = false;
    var shape = false;
    var col = false;
    function quantChange(){
        quant = true;
        if (quant == true && shape == true && col == true) {
            $('#done').show();
        }
    }
    function shapeChange() {
        shape = true;
        if (quant == true && shape == true && col == true) {
            $('#done').show();
        }
    }
    function colChange() {
        col = true;
        if (quant == true && shape == true && col == true) {
            $('#done').show();
        }
    }

I guess you should do something with .each() , like this working snippet:
(See comments in my code)

 $('.option').change(function() { $('#done').removeClass('nodisplay'); // By default, remove the class. $('.option').each(function() { // Then, for each .option… if (!$(this).val()) { // … if there's no option selected … $('#done').addClass('nodisplay'); // … add class, return; // … and stop loop! } }); }); 
 .nodisplay { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> {{# sentPt1 }} <div class="sent"> <div class="answer-container dropdown-container"> <p class="question"> <select id="response-quant" class="option" name="answer"> <option disabled selected></option> <option value={{ QUANT1 }}>{{ QUANT1 }}</option> <option value={{ QUANT2 }}>{{ QUANT2 }}</option> </select> {{ sentPt1 }} <select id="response-shape" class="option" name="answer"> <option disabled selected></option> <option value={{ SHAPE1 }}>{{ SHAPE1 }}</option> <option value={{ SHAPE2 }}>{{ SHAPE2 }}</option> <option value={{ SHAPE3 }}>{{ SHAPE3 }}</option> </select> {{ sentPt2 }} <select id="response-col" class="option" name="answer"> <option disabled selected></option> <option value={{ COL1 }}>{{ COL1 }}</option> <option value={{ COL2 }}>{{ COL2 }}</option> </select> {{ sentPt3 }} </p> <button id="done" class="nodisplay" onchange="doneChange">Done</button> </div> </div> {{/ sentPt1 }} 

Hope it helps.

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