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

查看:34
本文介绍了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 de vé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.

推荐答案

好的,经过一些研究,看起来 select option clik 由于浏览器不兼容而无法触发(看看这个 SO 问题:附加到选项节点的事件未被触发)

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天全站免登陆