即使没有更改,也会选择JQuery检测下拉列表值

编程入门 行业动态 更新时间:2024-10-09 00:46:38
本文介绍了即使没有更改,也会选择JQuery检测下拉列表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用JQuery或JavaScript,我想检测用户何时选择一个值,即使他们没有更改已经选择的值。

Using JQuery or JavaScript, I want to detect when a user selects a value, even if they don't change the already selected value.

这怎么可能是完成?

我试过 -

$('#MyID').select(function(){ /*my function*/ });

$('#MyID').change(function(){ /*my function*/ }); //nothing changing, so this fails

但它们不起作用。

示例 - 我有一个带有年份列表的下拉列表,没有选择任何内容,用户选择1976,我运行一个函数。选择1976后,用户再次点击下拉菜单并再次选择1976 ,我想再次运行该功能。

Example - I have a dropdown with a list of years in it with nothing selected, the user selects "1976", I run a function. With "1976" selected, the user clicks on the dropdown again and selects "1976" again, I want to run the function again.

推荐答案

所有系统(至少在第二次点击...)

all systems go (on second click at least...)

设置一个开关运行选择时,并在捕获后重置开关和/或模糊。

Set a switch to run on selection, and reset the switch after it's captured and/or on blur.

var go = false;//switch off $('#MyID').on('click',function() {//on click if (go) {//if go //do stuff here with $(this).val() go = false;//switch off } else { go = true; }//if !go, switch on }).on('blur', function() { go = false; });//switch off on blur

做了一个小提琴: jsfiddle/filever10/2XBVf/

编辑以获得键盘支持,这样的事情应该可行。

edit: for keyboard support as well, something like this should work.

var go = false;//switch off $('#MyID').on('focus active click',function() {//on focus/active doit($(this));//do it }).on('change', function() { go=true;//go doit($(this));//and doit }).on('blur', function() { go = false; });//switch off on blur function doit(el) { if (go) {//if go //do stuff here with el.val() go = false;//switch off } else {go=true}//else go }

做了一个小提琴: jsfiddle/filever10/ TyGPU /

更多推荐

即使没有更改,也会选择JQuery检测下拉列表值

本文发布于:2023-11-28 01:05:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640355.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:也会   列表   JQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!