jQuery选择相同值时选择更改事件 [英] jQuery select change event when selecting the same value

查看:103
本文介绍了jQuery选择相同值时选择更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何处理选择的折叠事件,或者即使选择的选项未更改也触发更改事件?

How can I handle the collapse event of a select, or trigger the change event even if the selected option did not change ?

搜索引擎,如果所选的选项相同,吉祥物仍将在其中移动。搜索引擎在此页面上: http://www.marocpneus.com

I need to have this for a search engine, where the mascot will still move if the option that was selected is the same. The search engine is on this page : http://www.marocpneus.com

例如,如果继续操作,然后单击第一个选择的 Type devéhicule,然后选择已经选择的 Tourisme,则角色将不会移至第二个选择。但是,如果您确实将 Tourisme更改为其他值之一,则该字符确实会使用经典的jQuery更改事件移动。

For example, if you go ahead and click on the first select "Type de véhicule" and choose "Tourisme" which was already selected, the character will not move to the second select. However if you do change "Tourisme" to one of the other values, the character will indeed move using the classic jQuery change event.

推荐答案

好吧,经过一些研究,似乎选择器clik由于浏览器不兼容而无法被触发(请看以下问题:附加到Option节点的事件未触发

Ok, after some research, it looks like select option clik cannot been fired due to browsers incomptability (look this SO question: Event attached to Option node not being fired)

...但是我们可以模拟点击进行解决,XD,您在这里有小提琴: http://jsfiddle.net/H9gg5/3

... but we can simulate the click, to make a workaround, XD, you have the fiddle here: http://jsfiddle.net/H9gg5/3

Js

$('select.click_option').click(function() {
    if ( $(this).data('clicks') == 1 ) {
        // Trigger here your function:    
        console.log('Selected Option: ' + $(this).val() );
        $(this).data('clicks', 0);
    } else {
        console.log('first click');
        $(this).data('clicks', 1);
    }
});

$('select.click_option').focusout( function() {
    $(this).data('clicks', 0);
});

HTML

<select class="click_option">
      <option value="1"> Selected 1 </option>
      <option value="2"> Selected 2 </option>
</select>

它是做什么的?好吧,我们知道我们已经选择了一个选项(甚至是相同的选项),因为我们在选择上单击了两次,因此,只需计算点击次数,然后在上一次点击之后触发XD。该代码还可以解决焦点丢失的问题,因为如果您单击选择之外的内容,它将在clicks = 1时关闭,并且必须将其重置。

What does it do? Well, we know we have selected an option (even the same option) because we click twice over the select, so, just count the number of clicks, and when it comes after a previous click, trigger it, XD. The code also handles the lose of focus, because if you click out of the select, it will close with clicks = 1 and you have to reset it.

我已经为选择添加了一个类,以便仅当用户单击所需的选择时才触发该功能。

I've added a class to the select, for triggering only the function when the user clicks the select that you want.

希望会有所帮助!

这篇关于jQuery选择相同值时选择更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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