繁体   English   中英

如何使用jQuery noty插件返回true或false?

[英]How do I return true or false using jQuery noty plugin?

我有以下jQuery使用noty插件和梅花购物车jQuery。 第一个代码块正确警告是/否并正确清空购物车。

第二个代码块使用noty正确显示yes / no消息但是它没有返回true / false,因此购物车没有清空。

我是jQuery的新手,所以我可能错过了一些明显的东西! 任何帮助,将不胜感激:

//This works..
emptycart: function () {
    if (!confirm('Are you sure you want to empty your cart?')) {
        return false;
    }
},

//This doesnt work..

emptycart: function () {
    confirm.call(this, noty({
        text: 'Are you sure you want to empty the cart ?',
        buttons: [
            {type: 'button green', text: 'Ok', click: function() { return false; } },
            {type: 'button pink', text: 'Cancel', click: function() { return true; } }
        ],
        closable: false,
        timeout: false
        }),
        true
    );
    return false;
}, 

好吧,插件正在接收回调,所以当你使用回调时,你必须以异步方式思考。

/**
 * @param {function} onConfirm : Receives true or false as argument, depending on the user choice
 */
emptycart: function (onConfirm) {
    confirm.call(this, noty({
        text: 'Are you sure you want to empty the cart ?',
        buttons: [
            {
                type: 'button green',
                text: 'Ok',
                click: function() {
                    //Do other stuff if you want...
                    onConfirm(true); 
                }
            },
            {
                type: 'button pink',
                text: 'Cancel',
                click: function() {
                    //Do other stuff if you want...
                    onConfirm(false);
                } 
            }
        ],
        closable: false,
        timeout: false
    }), true);
}

在上面,您将发送一个回调函数“onConfirm”,当用户点击任一按钮时,它将被调用。 该函数将接收一个布尔参数,告知是否单击“确定”或“取消”。

暂无
暂无

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

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