选择任何选择框选项(未更改)时的Jquery回调

时间:2014-10-10 07:21:51

标签: javascript jquery

我有一个下拉选择表单元素。选择一个选项后,我想做点什么。即使从下拉列表中选择的值是已经选择的选项。所以"在改变"在这种情况下不会工作。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

由于您的选择是"实际上"没有改变,jQuery更改事件不会自动触发。相反,你需要绑定它。使用@epoch建议的点击事件: -

示例代码: -

             //Intialize a local variable
             var x = 0;
             $('#single').click(function () {
                 //Increment local variable by 1.
                 x = x + 1;
                 //Since click event will be fired twice (once during focus & second while   selecting the option) compare '2' for same selection
                 if (x == 2) {
                     $(this).change();
                     x = 0;
                 }
             }).change(function () {
                 console.log(1);
                 //To reset the value of x back to 0, set it as -1 here (since click event will be fired after change).
                 x = -1;
             });