【问题标题】:Jquery callback when any select box option is chosen (not changed)选择任何选择框选项时的 Jquery 回调(未更改)
【发布时间】:2014-12-05 07:15:16
【问题描述】:

我有一个下拉选择表单元素。当一个选项被选中时,我想做点什么。即使从下拉列表中选择的值是已选择的选项。所以“on change”在这种情况下不起作用。

我该怎么做?

【问题讨论】:

标签: javascript jquery


【解决方案1】:

由于您的选项“实际上”没有改变,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;
             });

【讨论】:

  • 干杯,我认为这种通用方法是可行的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 2018-04-01
  • 2015-07-05
  • 1970-01-01
  • 2012-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多