jQuery Chosen not firing onchange event when value is changed through JS

大城市里の小女人 提交于 2019-12-05 03:38:41

You need to trigger the change after you change the value with $(selector).trigger("change")

$('#reset').click(function() {
        $("#chosen").val('').trigger("change"); //doesn't work
        //$("#chosen").prop('selectedIndex', 0).trigger("liszt:updated");
        console.log('reset: ' + $('option:selected').val());
    });
Tyler

Copying an answer out of the comments by Fabrício Matté that worked for me so others don't see this post and miss a solution at first like I did.

$('#chosen_chzn').remove();
$('#chosen').val('').change().removeClass('chzn-done').chosen();

See this on his Fiddle

I originally tried to continue using the .trigger('liszt:updated') call instead of the .change() but that did not work. Didn't see the reasoning behind .removeClass('chzn-done') either but this is also essential or your select box will vanish.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!