jQuery change() on <select> and firefox

后端 未结 5 1785
孤城傲影
孤城傲影 2020-12-06 05:04

I have a dropdown that triggers an ajax call when its changed:

$(\'.travel-to\').change(function(){  
    $.ajax({
        type: \"GET\",
        url: \"/inc         


        
5条回答
  •  独厮守ぢ
    2020-12-06 05:13

    I've just experienced some weird behavior in firefox which enables the user to open the select dropdown, navigate with the arrows and then instead of tabbing out or clicking on a side the user can click another element which will result into changing the select value without firing change event.

    To work around this you can trigger the change whenever the keyup (please also suggest other events?) event is fired and the select has a different value then the previous.

    var selectRegistry = {},
        $selects = $('select');
    
    $selects.bind('change', function() {
        var $this = $(this);
        selectRegistry[$this.attr('id')] = $this.val();
    });
    
    $selects.bind('keyup scroll select', function() {
        var $this = $(this);
        if ($this.val()!=selectRegistry[$this.attr('id')])
        {
            $this.trigger('change');
        }
    });
    

    You can use .live() function if you'll have dinamically created select elements along the runtime of the web page.

提交回复
热议问题