-2

有问题......谁能告诉我我在这段 JavaScript 代码上做错了什么:

function getname() {
    var the_name = window.prompt("Enter your first name", "");
    var first_char = the_name.charAt(0);

    if(first_char = "S") then {
        document.write(the_name);
    } else {
        window.alert("Please enter a name that starts with an uppercase S to have it displayed.");
    }
}
4

4 回答 4

1

then语法无效,在字符串比较时 使用==

function getname() 
{
var the_name=window.prompt("Enter your first name","");
var first_char = the_name.charAt(0);

if(first_char== "S") 
{
    document.write(the_name);
}
else
{
    window.alert("Please enter a name that starts with an uppercase S to have it displayed.");
}
}
于 2013-05-13T04:18:51.827 回答
0

尝试改变

if(first_char= "S")

if(first_char== "S")

JavaScript 比较和逻辑运算符

于 2013-05-13T04:17:04.500 回答
0

尝试使用==来比较您的价值,而不是=

if(first_char == "S") 

以及删除then您的 if-else 条件(实际上我认为这是您的解释:P)

于 2013-05-13T04:17:08.547 回答
0

语法中没有then 。if-else比较运算符==也不是=

function getname() 
{
var the_name=window.prompt("Enter your first name","");
var first_char = the_name.charAt(0);

if(first_char== "S") 
{
    document.write(the_name);
}
else
{
    window.alert("Please enter a name that starts with an uppercase S to have it displayed.");
}
}
于 2013-05-13T04:20:55.103 回答