【问题标题】:jQuery, trigger change also on already selected option [duplicate]jQuery,在已选择的选项上也触发更改[重复]
【发布时间】:2014-05-19 08:24:30
【问题描述】:

我的select 有问题。 我希望already selected 选项也触发.change() 功能。 目前它仅在选择另一个选项时触发。 如果没有太多 Soucrecode,我怎么能把这种可能性放在一起。也许已经有一种我不知道的可能性。

Here a fiddle with an example

编辑:

我也许应该提到它应该可以在移动浏览器上运行!

【问题讨论】:

  • 你能展示你的代码吗?,看看你尝试了什么。
  • 正如事件名称所表明的,更改只会在值更改时触发...
  • 问:为什么需要它重新触发选择已经选择的项目?从 UI 的角度来看,您不会期望事件发生,因为没有任何改变。
  • 谢谢@Mr.TK 我已经想通了...
  • @TrueBlueAussie 我需要它作为当前选项的刷新

标签: javascript jquery


【解决方案1】:

编辑: IE 不能很好地处理它! :(

要支持 IE,你可以使用:

--DEMO--

$(document).ready(function () {
    var con = $('#console');
    $('select').change(function () {        
        /* change also on already selected option */
        con.append('- ' + $(this).val() + '<br/>');
        //$(this).blur();
    }).change().bind('mousedown', function () {
        this.selectedIndex = -1;
        this.selectedIndex = $(this).find('option:selected').index();
    });
});

看看这是否符合您的需求:

--DEMO--

$(document).ready(function () {
    var con = $('#console');
    $('select').change(function () {        
        /* change also on already selected option */
        con.append('- ' + $(this).val() + '<br/>');
    }).change().bind('mousedown', function () {
        //on mousedown, clone SELECT element to display current selected value
        // this would be empty value otherwise when setting current selected index
        // NOTE: seems to be still not as good on IE...
        // FF would need to redefine some margin/padding
        if (!$(this).data('cloned')) $(this).data('cloned', $(this).clone().css({
            position: 'absolute',
            pointerEvents: 'none',
            top: this.offsetTop,
            left: this.offsetLeft,
            margin: 0
        }).appendTo('body'));
        // here set selectedIndex of SELECT element to not defined index
        // this would let on change event to be fired in all cases
        this.selectedIndex = -1;
    }).blur(function(){
        // on blur, remove cloned SELECT element and reset specific data object
        if ($(this).data('cloned')) {
            this.value = $(this).data('cloned').val();
            $(this).data('cloned').remove();
            $(this).data('cloned', null);
        }
    });
});

【讨论】:

  • 有趣...你能解释一下它是如何工作的吗?
  • @TrueBlueAussie 设置this.selectedIndex = -1 以触发任何选定选项的 onchange 事件。为 UI 使用克隆元素,否则 SELECT 反射值将为空。这需要更多测试以查看是否符合 OP 的需求。我不确定这是否能处理所有预期的情况
  • 我认为这符合我的需要。如果您可以在答案中解释您的代码,我将您的答案标记为正确。
  • 我不得不承认,这很聪明!
  • 不幸的是,IE 中的行为如此丑陋。点击反应和显示几乎无法使用。也许对 IE 进行一些调整以使其完美?
【解决方案2】:

by Daniel Eidson

function onSelectChange(){
    alert('changed');
};

var select2 = $("#select").select2().data('select2');

select2.onSelect = (function(fn) {
    return function(data, options) {
        var target;

        if (options != null) {
            target = $(options.target);
        }

        if (target) {
            onSelectChange();
            return fn.apply(this, arguments);
        }
    }
})(select2.onSelect);

DEMO

在 HTML 中:添加 id 以选择

<select id="select">
    <option selected>Selected</option>
    <option>Not selected</option>
</select>
<div id="console"></div>

【讨论】:

  • 这根本不起作用...在做出选择之前很快就会触发
  • 请查看demo url,你会发现它工作正常
  • 它在点击选择和选项时触发。 @TrueBlueAussie
  • OP 想要在用户更改选项或不更改选项时触发事件。所以根据我的说法,这是可以做到的。它也在演示中工作
  • 你能解释一下你在哪里定义.select2()函数吗?
猜你喜欢
  • 2013-10-12
  • 1970-01-01
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 2016-09-28
  • 2017-09-10
相关资源
最近更新 更多