Add values to a chosen multiselect

后端 未结 3 1108
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-07 06:16

I am wondering whether it\'s possible to add new values on the fly to a chosen.js multiselect (similar to how tagging works).

I\'ve seen in another SO post a user s

3条回答
  •  星月不相逢
    2021-01-07 07:05

    the documentation mentions this option:

    Updating Chosen Dynamically

    If you need to update the options in your select field and want Chosen to pick up the changes, you'll need to trigger the "chosen:updated" event on the field. Chosen will re-build itself based on the updated content.

    with this.

    $("#form_field").trigger("chosen:updated");
    

    This can be combined with

    // Add field
    $("#form_field").append("");
    
    $("#form_field").trigger("chosen:updated");
    

    Adding this together to an example can be found at the jsfiddle: http://jsfiddle.net/E5X9x/

提交回复
热议问题