gpt4 book ai didi

JavaScript Prompt() 输入

转载 作者:行者123 更新时间:2023-11-29 10:31:10 28 4
gpt4 key购买 nike

仅当用户在提示中单击取消 时,下面的代码才会运行alert('Please enter your name!');。如果用户未在提示框中输入任何内容并单击 Enter,我试图让代码也运行所述警报。但是,当他们什么都不输入并单击 Enter 时,代码会呈现“Hello”。我该怎么做?

var theName = prompt('What is your name?');

if(theName != null) {
document.write('Hello, ' + theName);
}

else{
alert('Please enter your name!');
}

最佳答案

Return value from prompt :

String. If the user clicks "OK", the input value is returned. If the user clicks "cancel", null is returned. If the user clicks OK without entering any text, an empty string is returned.;

关于JavaScript Prompt() 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45700404/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com