简体   繁体   English

通过jQuery取消选择按钮

[英]deselect button via jquery

I have a button, and I do a popover when a variable exceeds a stock variable: 我有一个按钮,当变量超过股票变量时,我会弹出一个窗口:

$(function () {
    $('[data-toggle="tooltip"]').tooltip({
        trigger: 'manual',
        placement: 'top',
        title: 'Ajouter un billet'
    })
    $('.plus').click(function () {
        var idP = $(this).attr('id');
        $.ajax({
            type: 'GET',
            url: Routing.generate('ajaxAddOneElemCart', {product_id: idP}),
            success: function (data) {
                var stock = data[2];
                $('.quantite' + idP).text(data[0]);
                if (stock > data[0]) {
                    $('.total').text(data[1]);
                } else {
                    $('.plus').attr('data-content', '<font color="red">ATTENTION: Il ne reste que ' + stock + ' billets disponibles. Vous ne pouvez pas en commander plus.</font>');
//                    $('[data-toggle="tooltip' + idP + '"]').popover();
                    $(this).tooltip('show');

                }
            }
        });
    });
    $('.moins').click(function () {
        var idP = $(this).attr('id');
        $.ajax({
            type: 'GET',
            url: Routing.generate('ajaxMinusElemCart', {product_id: idP}),
            success: function (data) {
                $('[data-toggle="tooltip' + idP + '"]').popover('destroy');
                $('.erreur' + idP).html('');
                $('.quantite' + idP).text(data[0]);
                $('.total').text(data[1]);
            }
        });
    });

});

HTML code: HTML代码:

<button tabindex="0" role="button" id="{{article[0].id}}" class="btn btn-succes plus"  data-toggle="tooltip">+</button>
<button type="button" id="{{article[0].id}}" class="btn btn-danger moins" title="Soustraire un billet">-</button>

$('[data-toggle="tooltip'+idP+'"]').popover() must be actioned when the data[0] is little as the variable stok. data[0]小于变量stok时,必须操作$('[data-toggle="tooltip'+idP+'"]').popover() popover $('[data-toggle="tooltip'+idP+'"]').popover() It works but: 它有效,但是:

在此处输入图片说明

If I click a various times over the + , it's stop at 50, but I haven't the popover. 如果我在+上多次单击,它会停在50,但我没有弹出式窗口。 If I click in another part of the screen and then click another time over the + , it opens the popover. 如果我单击屏幕的另一部分,然后在+上单击另一次,它将打开弹出窗口。

Have I to deselect the button? 我是否要取消选择按钮? How? 怎么样?

Thanks Best regards 最好的问候

I can see your problem. 我可以看到你的问题。 The $('[data-toggle="tooltip'+idP+'"]').popover() line simply activates the popover. $('[data-toggle="tooltip'+idP+'"]').popover()行仅激活弹出框。 The default trigger for popovers is a click so on, for example, the 50th click, the popover is activated but opens on the next click. 弹出窗口的默认触发器是单击,例如,第50次单击,弹出窗口被激活,但在下次单击时打开。 You also seem to be using a combination of tooltips and popovers, which are actually separate things. 您似乎还结合了工具提示和弹出窗口,它们实际上是分开的。 For your application of this code, I think popovers would be best. 对于您的该代码应用,我认为弹出窗口是最好的。 To fix this: 要解决此问题:

  1. Firstly, the plus and minus buttons will end up having the same ID, which is not valid. 首先,加号和减号按钮最终将具有相同的ID,这是无效的。 For my code, the buttons do not need specific ids so you could either remove them all together or, if you need them for other code, then make sure that you do not have two ids the same on one page. 对于我的代码,按钮不需要特定的ID,因此您可以将它们全部一起删除,或者,如果其他代码需要它们,则请确保在同一页面上没有两个ID。

  2. For the button's HTML, remove all the the data- attributes. 对于按钮的HTML,删除所有的data-属性。 I suggest defining these through JS instead, which is much neater. 我建议改为通过JS定义它们,这更加整洁。 Keep in mind that the title attribute of the plus button will become the title of the popover. 请记住,加号按钮的title属性将成为弹出框的标题。

  3. To show the popover, replace all the code in the else section of the plus success function with this. 要显示弹出窗口,请用此替换plus success函数的else部分中的所有代码。

     $(this).popover({ trigger: 'manual', placement: 'top', html: true, content: '<font color="red">ATTENTION: Il ne reste que ' + stock + ' billets disponibles. Vous ne pouvez pas en commander plus.</font>' }); $(this).popover('show'); 

    This initialises the plus button's popover with manual(JS only) trigger, placed above the element it's attached to with HTML enabled and your original content. 这会使用手动(仅适用于JS)触发器初始化加号按钮的弹出窗口,该触发器位于启用HTML的附加元素和原始内容的上方。 It then shows the popover. 然后显示弹出窗口。

  4. To hide the popover, put $(this).prev().popover('hide'); 要隐藏弹出窗口,请放入$(this).prev().popover('hide'); instead of the first line of your success function for the minus button. 而不是减号按钮的成功功能的第一行。 This will hide the popover of the previous element in the DOM, so ensure the plus button is immediately before the minus button in your HTML. 这将在DOM中隐藏上一个元素的弹出窗口,因此请确保加号按钮紧邻HTML中的减号按钮。

In the end that means your HTML is: 最后,这意味着您的HTML是:

<button tabindex="0" role="button" class="btn btn-succes plus" title="Ajouter un billet">+</button>
<button type="button" class="btn btn-danger moins" title="Soustraire un billet">-</button>

...and your JS is: ...而您的JS是:

$('.plus').click(function () {
    var idP = $(this).attr('id');
    $.ajax({
        type: 'GET',
        url: Routing.generate('ajaxAddOneElemCart', {
            product_id: idP
        }),
        success: function (data) {
            var stock = data[2];
            $('.quantite' + idP).text(data[0]);
            if (stock > data[0]) {
                $('.total').text(data[1]);
            } else {
                $(this).popover({
                    trigger: 'manual',
                    placement: 'top',
                    html: true,
                    content: '<font color="red">ATTENTION: Il ne reste que ' + stock + ' billets disponibles. Vous ne pouvez pas en commander plus.</font>'
                });
                $(this).popover('show');

            }
        }
    });
});

$('.moins').click(function () {
    var idP = $(this).attr('id');
    $.ajax({
        type: 'GET',
        url: Routing.generate('ajaxMinusElemCart', {
            product_id: idP
        }),
        success: function (data) {
            $(this).prev().popover('hide');
            $('.erreur' + idP).html('');
            $('.quantite' + idP).text(data[0]);
            $('.total').text(data[1]);
        }
    });
});

Hope this works, 希望这行得通,

Tugzrida. Tugzrida。

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

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