在Alertify提示中使用“[object Object]”代替变量字符串

时间:2013-07-03 02:45:11

标签: javascript jquery jquery-plugins alertify

我最近从常规JavaScript提示切换到jQuery插件'Alertify'提示。但是,当我将变量定义为提示的响应,并在稍后的警报中使用该变量时,它会打印'[object Object]'。经过几个小时的谷歌搜索和测试后,我仍然没有得到答案。

这是我的代码:

do {
    var fullLoop = false;
    var user = alertify.prompt("What is your name?", function (e, str) {
        if (e) {
            user.toUpperCase();
            if (str.length === 0) {
                alertify.alert("Please enter your name.");
                fullLoop = true;
            }
        } else {
            alertify.alert("Please enter your name.");
            fullLoop = true;
        }
    }, "Name");
    do {
        var bestLoop = false;
        var best = alertify.prompt("So " + user + ", what is your greatest ability, STRENGTH, SPEED, or SMARTS?", function (e, str) {
        if (e) {
            best.toUpperCase();
            if (best === "STRENGTH" || best === "SPEED" || best === "SMARTS") {
            var offset = options.indexOf(best);
            if (offset != -1) {
                options.splice(offset, 1);
            }
        } else {
            alert("Please choose either STRENGTH, SPEED, or SMARTS as your greatest ability.");
            bestLoop = true;
        }
        } else {
            alertify.alert("Please choose either STRENGTH, SPEED, or SMARTS as your greatest ability.");
            bestLoop = true;
        }
    }, "Greatest Ability");
    } while (bestLoop);
    alertify.alert("Great! So " + best + " is yor greatest ability.");
} while (fullLoop);

您可以看到我遇到的错误here

我也注意到我的关于字符串的if语句被忽略了,我假设有一个连接,但它是什么?我没有发现任何错误,所以我使用Alertify不正确吗?

2 个答案:

答案 0 :(得分:0)

我对alterify了解不多,但看起来你正试图显示一个JSON对象。尝试JSON.stringify(用户),看看你得到了什么。它可能类似于

{ "Name": "Paul" }

如果是这样,那么你就想做

alertify.prompt("So " + user.Name + ", what is your greatest ability, STRENGTH, SPEED, or SMARTS?"

或者

alertify.prompt("So " + JSON.stringify(user.Name) + ", what is your greatest ability, STRENGTH, SPEED, or SMARTS?"

希望这有帮助!

答案 1 :(得分:0)

您对alertify插件的使用是错误的,提示方法是一种异步方法,所有使用它的操作都必须在回调中完成。

缩小版本如下

var options = ["STRENGTH", "SPEED", "SMARTS"];
alertify.alert("Before we begin our journey, let's learn a little bit  about you.");

alertify.prompt("What is your name?", function (e, str) {
    if (e) {
        var  user = str.toUpperCase();

        alertify.prompt("So " + user + ", what is your greatest ability, STRENGTH, SPEED, or SMARTS?", function (e, str) {
            if (e) {
                var best = str.toUpperCase();
                if (best === "STRENGTH" || best === "SPEED" || best === "SMARTS") {
                    var offset = options.indexOf(best);
                    if (offset != -1) {
                        options.splice(offset, 1);
                    }
                    alertify.alert("Great! So " + best + " is yor greatest ability.");        
                } else {
                }
            } else {
            }
        }, "Greatest Ability");
    } else {
    }
}, "Name");

演示:Fiddle