我正在尝试制作这个Javascript,这样当我的网页打开时,它会要求输入密码和你的名字,如果你没有输入你的名字,它会在你做的时候进行循环,并将第一个字母大写输入的名称。但它不会把第一个字母大写。请帮忙。谢谢。
function startFunction() {
var Password = prompt("What is the password?");
getPassword(Password);
};
function getPassword(Password) {
if (Password == "123") {
alert("Correct password. Access granted.");
var Name = prompt("What is your name?");
getName(Name);
}
else {
alert("Incorrect password. Access denied.");
window.close();
}
};
function getName(Name) {
if (Name.length === 0) {
while (Name.length === 0) {
alert("Please enter your name.");
var Name = prompt("What is your name?");
if (Name.length > 0) {
alert("Nice to meet you, " + Name + ".");
}
}
}
else {
try {
var newName2 = Name.slice(1, Number(name.length));
var newName1 = Name.slice(0, 1);
var newName = newName1.toUpperCase() + newName2;
alert("Nice to meet you, " + newName + ".");
}
catch (ValueError) {
alert("Hello")
}
}
};
startFunction();
答案 0 :(得分:0)
你的while循环没有包含大写名字第一个字母的逻辑,所以如果你第一次尝试输入名字是空的,那么它的问候语就不会大写。
我简化了<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/mathquill/0.10.1/mathquill.min.css">`
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathquill/0.10.1/mathquill.min.js" type="text/javascript"></script>
并将大小写移动到它自己的函数:
getName()
它在行动中的一个小提琴:https://jsfiddle.net/21mdLfrb/
答案 1 :(得分:0)
你去吧
Html:
<input type="text" id="name" ></input>
<input type="password" id="pass" ></input>
<button onclick="process()" >
Submit
</button>
Javascript:
function process(){
var getName= document.getElementById("name").value;
var getPass= document.getElementById("pass").value;
if(getName && getPass){
getName = getName.charAt(0).toUpperCase() + getName.slice(1);
alert("Nice to meet you, " + getName + ".");
}
}