【问题标题】:Javascript/JQ - Confim - true/false responseJavascript/JQUERY - 确认 - 真/假响应
【发布时间】:2014-06-26 18:35:34
【问题描述】:

我正在尝试根据 javascript 确认对话框的结果创建一个条件函数。 无论我点击什么,它似乎都会返回 true。有人看到我做错了吗?

$(function () {
    $("#Language").change(function () {
        var a = $(this).val();
        if (a == 3) {
            confirm("Selecting a bilingual calendar will effect the billing. ")
            if (confirm) { console.log("test"); }
        }

    });
});

【问题讨论】:

  • 别担心,马克。这是一个体面的问题。您发布了您尝试过的内容,问题是什么,并且很简短。你有我的 +1。

标签: javascript jquery


【解决方案1】:

if(confirm) 真的没有为你做任何事情(因为它不存在)。试试这个:

// Save the response in a var called userResponse
var userResponse = confirm("Selecting a bilingual calendar will effect the billing. ")
if (userResponse) { console.log("test"); }

您还可以通过简单地将确认放在您的 if 语句中来稍微缩短代码:

// confirm() returns true or false. So, when evaluated your if simply says
// if(true) or if(false), depending on the answer.
if (confirm("Selecting a bilingual calendar will effect the billing. ")) {
    console.log("test");
}

【讨论】:

  • 使用“confirm”作为变量名是一种危险的做法。尝试“响应”或“回答”。或者,只需将确认方法调用移动到 if 语句中。
  • @Beez,你说得对。我一瞬间失去了理智。已更新。
  • 答案来了,还要等8分钟
【解决方案2】:
$(function () {
    $("#Language").change(function () {
        var a = $(this).val();
        if (a == "3") { // notice the quotation marks
            // notice this variable
            var confirmed = confirm("Selecting a ... billing.");
            if (confirmed) { console.log("test"); }
        }
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-10
    • 2018-03-19
    • 2014-10-25
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多