我有这个javascript提示,如果它不是空的它连接到服务器,如果它是空的,或者用户点击取消,我想再次返回到启动预告。我不希望访问者能够加入。我该怎么办?
var nick = prompt("Please enter your name", "")
if (nick != "");
{
$.connection.chathub.server.sendnick(nick);
}
else
{
// go back to start prompter again
}
答案 0 :(得分:1)
使用while循环 http://www.w3schools.com/js/js_loop_while.asp
var nick ="";
while (nick == "");
{
nick = prompt("Please enter your name", "");
}
$.connection.chathub.server.sendnick(nick);
答案 1 :(得分:1)
似乎你需要循环,循环在条件为真时做某事。
var nick = prompt("Please enter your name", "");
while (nick == "" || nick == null) {
nick = prompt("Please enter your name", "");
}
$.connection.chathub.server.sendnick(nick);