简体   繁体   中英

Event listener for select elements that has not changed values

My situation is as follows:

I have got a select box with a few options. One of those options can be preselected on pageload by the back end:

<select name="select" class="js-choice-select">
    <option value="option-1">Option 1</option>
    <option value="option-2" selected="selected">Option 2</option>
    <option value="option-3">Option 3</option>
    <option value="option-4">Option 4</option>
    <option value="option-5">Option 5</option>
</select>

Whenever this select element changes value, some JS code needs to be triggered. So, in this case with jQuery I add the following code:

$(".js-choice-select").on("change", function() {

    var selectedValue = $(this).val();

    doSomething(selectedValue);

});

PROBLEM

Now when the user opened the select box, but doesn't select any other value than the already selected option, the default 'change' event listener will not trigger, but my doSomething function needs to be fired.

Does anyone know a feasible and straight-forward solution for this?

You could trigger the same piece of code on click , focus and/or blur events. This way doSomething() will be triggered whenever you need it to.

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