jQuery选择相同选项上的更改事件 [英] jquery select change event on the same option

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

问题描述

这是我的简单代码:

$(".test").change(function(){
    alert("user clicked");
});

<select class="test">
    <option value="1">test1</option>
    <option value="2">test2</option>
</select>

这一切都很简单而且可行,但是我不仅希望在用户更改选项时调用该函数,而且还希望当他单击已经选择的相同选项时调用该函数,我尝试了click事件,但是它被触发了在用户甚至没有单击任何选项之前,我该怎么办?

解决方案

不适用于chrome.

$(".test option").click(function(e){
    console.log('click');
});

更新了小提琴.

点击选项

看起来好像没有一堆工作就不可能完成,例如使用单击选择并比较被单击的位置或类似的东西.

我不确定最终目标是什么,但是如果您可以在最初单击时多做一次处理,则单击选择可能会起作用.

另一篇文章:

Here is my simple code:

$(".test").change(function(){
    alert("user clicked");
});

<select class="test">
    <option value="1">test1</option>
    <option value="2">test2</option>
</select>

It's all simple and working, but I want to get that function called not only when user changes option, but when he clicks on the same option that is already selected, I've tried click event but it gets fired up before user even clicka on any option, what could I do?

解决方案

doesn't work in chrome.

$(".test option").click(function(e){
    console.log('click');
});

updated your fiddle.

put the click on the option

EDIT: looks like it isn't possible without a bunch of work like using click on the select and comparing the location it was clicked or something hokey like that.

I am not sure what the end goal is, but the click on the select might work if you can do the processing one extra time when they initially click into it.

another article on it:

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

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