简体   繁体   English

select2.js 维护先前点击的项目,这导致在确认弹出窗口中删除多个项目

[英]select2.js maintain previously clicked items which results in remove more than one items on confirmation popup

I have one textbox in which i can add and remove items using select2.js .我有一个文本框,我可以在其中使用select2.js添加和删​​除项目。 Now i have one requirement that when i remove item from textbox, it should ask for some confirmation message.现在我有一个要求,当我从文本框中删除项目时,它应该要求一些确认消息。

For that i need to make a change in unselect() method of select2.js .为此,我需要对select2.js unselect()方法进行select2.js Below is the code in which i have made change.以下是我进行更改的代码。

unselect: function (b) {
    var $curelm = this;
    var d = "", e= "", c = "";
    confirmpopup("Confirm", "Are you sure you want to remove this Tag?", function (confirm) {   //  <-- Here i have added confirmpopup code
        if (confirm) {
            c = $curelm.getVal();
            if (b = b.closest(".select2-search-choice"), 0 === b.length) throw "Invalid argument: " + b + ". Must be .select2-search-choice";
            if (d = b.data("select2-data")) {
                var f = a.Event("select2-removing");
                if (f.val = $curelm.id(d), f.choice = d, $curelm.opts.element.trigger(f), f.isDefaultPrevented()) return !1;
                for (;
                    (e = p($curelm.id(d), c)) >= 0;) c.splice(e, 1), $curelm.setVal(c), $curelm.select && $curelm.postprocessResults();
                return b.remove(), $curelm.opts.element.trigger({
                    type: "select2-removed",
                    val: $curelm.id(d),
                    choice: d
                }), $curelm.triggerChange({
                    removed: d
                }), !0
            }
        }
    });
},

I have prepare one JS Fiddle to show you my problem.我准备了一个JS Fiddle来向你展示我的问题。

When i click on "x" (Remove sign) in the box, it'll open one confirm popup and in that popup if i click on "Yes" button then it'll perfectly remove item from the box and when i click on "No" button, it'll not remove item.当我单击框中的"x" (删除符号)时,它会打开一个确认弹出窗口,如果我单击"Yes"按钮,它将在该弹出窗口中完美地从框中删除项目,当我单击"No"按钮,它不会删除项目。 Upto now everything is fine.到现在一切都很好。 Problem is arise now, when second time i click on "x" in the box and click on "Yes" button then it'll remove 2 items from the box rather than remove only selected items.现在出现问题,当我第二次单击框中的"x"并单击"Yes"按钮时,它将从框中删除 2 个项目,而不是仅删除选定的项目。 I don't know why, but it'll maintain previous clicked items history( I guess ).我不知道为什么,但它会保留以前点击的项目历史记录(我猜)。

Can any one point out what i have made wrong in the above code ?谁能指出我在上面的代码中做错了什么?

To generate the issue in JS Fiddle follow below steps:要在JS Fiddle 中生成问题,请按照以下步骤操作:

  • Select more than two items.选择两个以上的项目。
  • Click on "x" button on selected item and in confirm popup click on "No" button.单击所选项目上的"x"按钮,然后在确认弹出窗口中单击"No"按钮。
  • Now again click on "x" button on another item and now click on "Yes" button on confirm popup.现在再次单击另一个项目上的"x"按钮,然后在确认弹出窗口中单击"Yes"按钮。
  • It'll remove two items rather than only selected one.它将删除两个项目,而不是仅选择一个。

NOTE: I have added the whole select2.js code in JS Fiddle rather than add it in external link because i have made change in its unselect method.注意:我已在 JS Fiddle 中添加了整个select2.js代码,而不是将其添加到外部链接中,因为我对其unselect方法进行了更改。

Okay, I got the solution after doing some deep digging in my code.好的,我在深入挖掘我的代码后得到了解决方案。 The problem in confirmpopup() function. confirmpopup()函数中的问题。 When i click on item to remove from select2 box at that time i have registered click event for "Yes" button.当我单击要从 select2 框中删除的项目时,我已为"Yes"按钮注册了单击事件。 Then again i click on another item to remove from select2 box and again "Yes" and "No" button event registered of confirm popup in DOM.然后我再次单击另一个项目以从 select2 框中删除,并再次在 DOM 中注册确认弹出窗口的"Yes""No"按钮事件。 So this is the reason "Yes" button click event called 2 times.所以这就是"Yes"按钮点击事件被称为 2 次的原因。

So to overcome to this problem, i have to first unbind click event using .Off() method in the code like below.因此,为了克服这个问题,我必须首先在如下代码中使用.Off()方法解除点击事件的绑定

function confirmpopup(title, message, callbackfunc) {
    $('#errorlabel').text(title);
    $('#popupmessage').text(message);
    $('#modelconfirm').show();

    $('#btnyes').off('click'); // <-- Solution is here

    $('#btnyes').on('click', function () {
        $('#modelconfirm').hide();
        callbackfunc();
    });

    $('#btnno, .close').on('click', function () {
        $('#modelconfirm').hide();
    });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM