简体   繁体   English

未更改值的选择元素的事件侦听器

[英]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. 每当此select元素更改值时,都需要触发一些JS代码。 So, in this case with jQuery I add the following code: 因此,在这种情况下,使用jQuery时,我添加了以下代码:

$(".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. 现在,当用户打开选择框,但除了已选择的选项之外未选择任何其他值时,默认的“ change”事件侦听器将不会触发,但需要触发我的doSomething函数。

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. 您可以在clickfocus和/或blur事件上触发相同的代码。 This way doSomething() will be triggered whenever you need it to. 这样,只要需要, doSomething()就会被触发。

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

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