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

时间:2012-03-10 20:16:01

标签: javascript jquery jquery-plugins

我使用noty插件和梅花购物车jQuery有以下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;
}, 

1 个答案:

答案 0 :(得分:1)

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

/**
 * @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”,当用户点击任一按钮时,它将被调用。该函数将接收一个布尔参数,告知是否单击“确定”或“取消”。

相关问题