在下拉列表中选择相同的值时触发事件? [英] Trigger the event when selected the same value in dropdown?

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

问题描述

问题:我有一个下拉列表,其中有年份列表,但未选择任何内容,用户选择"1976",我运行一个函数.如果用户再次单击下拉菜单并再次选择"1976",我想再次运行该功能.

Issue: I have a dropdown with a list of years in it with nothing selected, the user selects "1976", I run a function. If the user clicks on the dropdown again and selects "1976" AGAIN, I want to run the function again.

$('select').on('change', function (e) 
{
var optionSelected = $("option:selected", this);
var valueSelected = this.value; 
alert(valueSelected);      
});

推荐答案

Simple JS
---------

<html>
<head>
<script>
var prevIndex = "";

function onSelect()
{
    var currIndex = document.getElementById("ddList").selectedIndex;
    if( currIndex > 0 )
    {
        if( prevIndex != currIndex )
        {
            alert("Selected Index = " + currIndex);
            prevIndex = currIndex;
        }
        else
        {
            prevIndex = "";
        }
    }
}
</script>
</head>
<body>
    <select id="ddList" onClick="onSelect()">
        <option value="0">Select Me</option>
        <option value="1">List1</option>
        <option value="2">List2</option>
        <option value="3">List3</option>
    </select>
</body>
</html>

这篇关于在下拉列表中选择相同的值时触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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