How do I fire a new ajax on select2 new /remove tag event?

前端 未结 2 977
北荒
北荒 2020-12-11 04:27

I use the following snippet to add a new select2 tag remotely with ajax and I want to register or remove some records of my many to many table on new tag / remove tag event<

2条回答
  •  甜味超标
    2020-12-11 05:17

    Is there a fiddle where you can post a version of this problem.

    Based on what I understood, would the following pattern suffice?

      function dynamicSelect2(id) {
          $.ajax({
              url: 'data-url',
              data: 'parameters',
              dataType: 'json'
          }).done(function () {
              //Create the Select2 with necessary data on the element "id" passed.
          }).always(function () {
              //Attach other events..
          });
      }
    

    It is possible to create the entire select2 box dynamically and attach events on it this way. If you do that within a closure, you'll have access to variables that you defined prior to your ajax calls.

提交回复
热议问题