如果单击已选择的选项,则触发更改事件 [英] Trigger change event if I click on already selected option

查看:47
本文介绍了如果单击已选择的选项,则触发更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果单击已选择的选项,如何触发更改事件.例如,我在下拉列表中有以下选项

How can I trigger change event if I click on already selected option. For example I have following options in dropdown

  • 红色
  • 蓝色
  • 绿色

并且选择了蓝色选项.现在我希望,如果我再次单击蓝色项目,则应该触发更改事件.

and Blue option is selected. Now I want that if I click again on blue item then the change event should fire.

推荐答案

运行 change 即使用户选择了所选选项也是一个已知问题 - 您可以 阅读此处了解它.

Running the change even when the user selects the selected option is a known problem - you can read here about it.

现在,当您知道如何在用户选择任何选项时触发事件时,您只需存储上次使用的值并检查是否选择了相同的项目.

Now, when you know how to trigger an event whenever a user selects any option, you can simply store the last used value and check if the same item is selected.

var previousValue;
$("select")
    .mouseup(function() {
        var open = $(this).data("isopen");

        if (open) {
            if (this.value === previousValue)
            {
                alert("The same select has been clicked");
            }
        }

        previousValue = this.value;
    
        $(this).data("isopen", !open);
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
  <option>Red</option>
  <option selected>Green</option>
  <option>Blue</option>
</select>

这篇关于如果单击已选择的选项,则触发更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆