目前正在学习如何工作提示。当用户在我的某个提示中输入false值时,它会终止脚本并显示空白页面。而不是空白,我想编程重新启动提示让用户再次尝试而不必刷新页面。有什么提示吗?
较短版本的代码:
var subjectGender="";
var genderOK=true;
var originalGendertag="male";
var newGendertag="female";
subjectName=prompt("What is the subject's name?",subjectName);
subjectGender=prompt("Is "+subjectName+" male(M) or female(F)?","M or F");
if ((subjectGender!="M")&&(subjectGender!="F")){
alert("Must enter M or F");
genderOK=false;
originalGendertag="female";
newGendertag="male";
}
if (genderOK==true){
document.write(subjectName+" is "+originalGendertag+".")
}
答案 0 :(得分:0)
如果用户没有输入您期望的值,只需要进行递归函数调用。
// Call the function
showPrompt();
function showPrompt() {
var age = prompt("Please enter your age: ");
// The prompt will be shown until the user hits 24 in this case
if (+age !== 24) {
showPrompt();
} else {
console.log('Close my prompt !!');
}
}
<强> Example 强>