开发者

trigger a 'change' action when a dropdown is 'selected' not when the dropdown list values are changed

开发者 https://www.devze.com 2023-03-13 14:12 出处:网络
I\'ve got a few dropdown lists which are created dynamically. I\'ve done a decent job of keeping my code DRY, but I have a small problem.

I've got a few dropdown lists which are created dynamically.

I've done a decent job of keeping my code DRY, but I have a small problem.

I instruct jQuery to trigger an event when a select list 'changes', and seeing as the list are built dynamically, they change when a response is retrieved from the server, which jQuery is considering a change. But what I actually want is for the action to be triggered only when an option is 'selected' from the dropdown list.

$('select').change(function(){
       var idx = $(this).index();
       var query = $(this).val();
    $.ajax({
        url: 'getNext',
        data: {query:query},
        dataType: 'json',
        success: function(data){
                    var optionsList =''; 
                      for (var j=0; j'+data[j]+'';
                        }
                     $('select:eq('+idx+1+')').html(optionsList);
                         }
     });
});

I've tried using

$('select:selected').change()
but that doesn't trigger anything. Any suggestions on how to only trigger change when an option is selected, n开发者_如何学编程ot when an option or select list is created?


Have you tried:

$('select').click(
    function(){
        $(this).trigger('change');
    });


Try trigger

$('select:selected').trigger('change'); 
0

精彩评论

暂无评论...
验证码 换一张
取 消