Javascript/JQ -确认-真/假响应



我正试图根据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"); }
        }
    });
});

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来缩短代码:

// 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");
}
$(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"); }
        }
    });
});

最新更新