【问题标题】:How do I return true or false using jQuery noty plugin?如何使用 jQuery noty 插件返回 true 或 false?
【发布时间】:2012-03-10 20:16:01
【问题描述】:

我有以下使用 noty 插件和李子购物车 jQuery 的 jQuery。第一个代码块正确地提醒是/否并正确清空购物车。

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

我对 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;
}, 

【问题讨论】:

  • 你能做一个jsfiddle吗?

标签: javascript jquery jquery-plugins


【解决方案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”,当用户单击任一按钮时将调用该回调函数。该函数将接收一个布尔参数,告知是单击确定还是取消。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-14
    • 2013-11-05
    • 2011-01-17
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    相关资源
    最近更新 更多