即使重新选择了相同选项,也可以运行更改事件以进行选择 [英] Run change event for select even when same option is reselected

查看:73
本文介绍了即使重新选择了相同选项,也可以运行更改事件以进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有下拉菜单,看起来像这样:

 < select> 
< option> 0< / option>
< option selected =selected> 1< / option>
< option> 2< / option>
< option> 3< / option>
< / select>

我试图编写一个即使在选择相同选项时也会被解雇的函数,打开下拉菜单并重新选择所选选项,我希望它执行该功能。

解决方案

如果你的意思是用鼠标选择,你可以使用 mouseup 。然而,当选择框被打开时它也会触发,所以你需要跟踪它被触发的次数(even:select被打开,odd:select被关闭): http://jsfiddle.net/T4yUm/2/

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

if(open ){
alert(1);
}

$(this).data(isopen,!open);
});


Basically, I have drop down menu that looks like this:

<select>
  <option>0</option>
  <option selected="selected">1</option>
  <option>2</option>
  <option>3</option>
</select>

I am trying to write a function that is fired even when you select the same option, i.e. even if the drop-down is opened and re-select the selected option, I want it to execute the function.

解决方案

If you mean selecting with the mouse, you can use mouseup. However, it will fire when the select box is being opened as well, so you'll need to keep track of the amount of times it was fired (even: select is being opened, odd: select is being closed): http://jsfiddle.net/T4yUm/2/.

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

    if(open) {
        alert(1);
    }

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

这篇关于即使重新选择了相同选项,也可以运行更改事件以进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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