Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何在select2 new/remove tag事件上启动新的ajax?_Jquery_Ajax_Twitter Bootstrap_Jquery Select2 - Fatal编程技术网

Jquery 如何在select2 new/remove tag事件上启动新的ajax?

Jquery 如何在select2 new/remove tag事件上启动新的ajax?,jquery,ajax,twitter-bootstrap,jquery-select2,Jquery,Ajax,Twitter Bootstrap,Jquery Select2,我使用下面的代码片段使用ajax远程添加一个新标记,并希望在new tag/remove tag事件上注册或删除我的多对多表的一些记录 这张桌子看起来像 --------------------------------- +--voucher_id--+|+--product_id--+ --------------------------------- + 123 | 566 + --------------------------------- +

我使用下面的代码片段使用ajax远程添加一个新标记,并希望在new tag/remove tag事件上注册或删除我的多对多表的一些记录

这张桌子看起来像

---------------------------------
+--voucher_id--+|+--product_id--+
---------------------------------
+     123       |   566         +
---------------------------------
+     156       |   566         +
---------------------------------
+     123       |   426         +
---------------------------------
+     156       |   516         +
---------------------------------
我的Javascript

$(".e6").select2({
    tags: true,
    placeholder: 'placeholder',
    minimumInputLength: 1,

    ajax: {
        url: 'searchProducts',
        dataType: 'json',
        data: function(term) {
            return {q: term};
        },
        results: function(data) {
            return {results: data};
        }
    },
    createSearchChoice: function(term, data) {
        if ($(data).filter(function() {
            return this.computername.localeCompare(term) === 0;
        }).length === 0) {
            return {id: term, name: term};
        }
    },
    formatResult: function(item, page) {
        return item.computername;
    },
    formatSelection: function(item, page) {
        return item.computername;
    }
});
在返回的json中,我也有一个产品ID,我正在寻找一种在select2事件上触发新ajax的方法,但我不知道应该在哪里保存或删除表中的数据

做了一些研究后,我已经能够构建一个函数来更新上表中的记录,并且目前运行良好

$('.e6').on("change", function(e){                           
    console.log(ids);
    console.log(gs);
    $.ajax({
        type: "POST",
        url: '/admin/?controller=vouchers&action=updateRelatedProducts',
        data: {ids: ids, gs:gs},
        error: function () {
            alert("error");
        }
    });                   
});

但是,我在用初始的现有标记填充输入字段时遇到了问题。

是否有一个小提琴,您可以在那里发布此问题的版本

根据我的理解,以下模式是否足够

  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..
      });
  }
可以动态创建整个select2框,并以这种方式在其上附加事件。
如果在闭包中执行此操作,则可以访问在ajax调用之前定义的变量。

未测试,但应该可以:

$('.e6').on("change", function(e){
    if (e.removed) {
        $.ajax({
            type: "POST",
            url: '/admin/?controller=vouchers&action=updateRelatedProducts',
            data: {id: e.removed.id, action: remove},    //Or you can e.removed.text
            error: function () {
                alert("error");
            }
        });
    }
    if (e.added) {
        $.ajax({
            type: "POST",
            url: '/admin/?controller=vouchers&action=updateRelatedProducts',
            data: {id: e.added.id, action: add},    //Or you can e.added.text
            error: function () {
                alert("error");
            }
        });
    }

    //OR you can play with val data instead
    if (e.val) {
        $.ajax({
            type: "POST",
            url: '/admin/?controller=vouchers&action=updateRelatedProducts',
            data: {val: JSON.stringify(e.val)},    //Will send all the selected values
            error: function () {
                alert("error");
            }
        });
    }
}

将ajax函数抽象为它自己的函数,然后将其应用于您必须执行的任何select2函数。我不清楚在select2I的情况下如何捕获remove或add事件我刚才看了
select2
,它太棒了。我们必须开始使用它@它实际上很像我提出了一个问题,你能看一下吗。