1

我之前上了一学期的 JavaScript 课程,通过了,现在我正在放暑假。我们使用的书是 JavaScript 8th edition,Tom Negrino,Dori Smith。有一个名称生成器的代码。所需的一切都在书中(第 301 页)它需要名字和姓氏,但我想添加一个中间名来输出另一个愚蠢的名字。这不是家庭作业,只是我想尝试的事情。如果您将名字或姓氏留空,您会收到“这不是一个有效的名字”消息。所以我对中间名进行了编码,输入了 26 个单词作为中间名,但代码除了名字和姓氏之外什么都不识别。对我来说,这看起来应该有效,但仍然只有名字和姓氏有效。也许另一双眼睛可以帮助我看到我错过或没有包括在内的东西。

感谢您的任何建议,CK

The HTML
<!DOCTYPE html>
<html>
<head>
<title>Silly Name Generator</title>
<script src="script05.js"></script>
</head>
<body>
<h1>What's your silly name?</h1>
<table>
<tr>
    <td class="rtAlign">First Name:</td>
    <td><input type="text" id="fName" size="30"></td>
</tr>
<tr>
    <td class="rtAlign">Middle Name:</td>
    <td><input type="text" id="mName" size="30"></td>
</tr>
<tr>
    <td class="rtAlign">Last Name:</td>
    <td><input type="text" id="lName" size="30"></td>
</tr>
<tr>
    <td>&nbsp;</td>
    <td><input type="submit" value="Submit" id="sillySubmit">
</tr>
</table>
<p id="msgField">&nbsp;</p>
</body>
</html>

JavaScript

window.onload = initAll;

function initAll() {
document.getElementById("sillySubmit").onclick = function() {
    document.getElementById("msgField").innerHTML = getSillyName();
    return false;
}
}

function getSillyName() {
var firstName = ["Runny", "Buttercup", "Dinky", "Stinky", "Crusty",
"Greasy","Gidget", "Cheesypoof", "Lumpy", "Wacky", "Tiny", "Flunky",
"Fluffy", "Zippy", "Doofus", "Gobsmacked", "Slimy", "Grimy", "Salamander",
"Oily", "Burrito", "Bumpy", "Loopy", "Snotty", "Irving", "Egbert"];

var middleName =["Waffer", "Lilly","Rugrat","Sand", "Fuzzy","Kitty",
 "Puppy", "Snuggles","Rubber", "Stinky", "Lulu", "Lala", "Sparkle", "Glitter",
 "Silver", "Golden", "Rainbow", "Cloud", "Rain", "Stormy", "Wink", "Sugar",
 "Twinkle", "Star", "Halo", "Angel"];

var lastName1 = ["Snicker", "Buffalo", "Gross", "Bubble", "Sheep",
 "Corset", "Toilet", "Lizard", "Waffle", "Kumquat", "Burger", "Chimp", "Liver",
 "Gorilla", "Rhino", "Emu", "Pizza", "Toad", "Gerbil", "Pickle", "Tofu", 
"Chicken", "Potato", "Hamster", "Lemur", "Vermin"];
var lastName2 = ["face", "dip", "nose", "brain", "head", "breath", 
"pants", "shorts", "lips", "mouth", "muffin", "butt", "bottom", "elbow", 
"honker", "toes", "buns", "spew", "kisser", "fanny", "squirt", "chunks", 
"brains", "wit", "juice", "shower"];

var firstNm = document.getElementById("fName").value.toUpperCase();
var middleNm = document.getElementById("mName").value.toUpperCase();
var lastNm = document.getElementById("lName").value.toUpperCase();
var validName = true;

if (firstNm == "") {
    validName = false;
}
else {
    var firstNum = firstNm.charCodeAt(0) - 65;

    if (firstNum < 0 || firstNum > 25) {
        validName = false;
    }
}

if (!validName) {
    document.getElementById("fName").focus();
    document.getElementById("fName").select();
    return "That's not a valid first name";
}
if (middleNm == "") {
    validName = false;
}
else {
    var middleNum = middleNm.charCodeAt(0) - 65;

    if (middleNum < 0 || middleNum > 25) {
        validName = false;
    }
}
if (!validName) {
    document.getElementById("mName").focus();
    document.getElementById("mName").select();
    return "That's not a valid middle name";
}

if (lastNm == "") {
    validName = false;
}
else {
    var lastNum1 = lastNm.charCodeAt(0) - 65;
    var lastNum2 = lastNm.charCodeAt((lastNm.length-1)) - 65;

    if (lastNum1 < 0 || lastNum1 > 25 || lastNum2 < 0 || lastNum2 > 25) {
        validName = false;
    }
}

if (!validName) {
    document.getElementById("lName").focus();
    document.getElementById("lName").select();
    return "That's not a valid last name";
}

return "Your silly name is " + firstName[firstNum]
 + " " +  middleName[middleNum] + " " + lastName1[lastNum1]
 + lastName2[lastNum2];
 }
4

1 回答 1

1

你的 HTML 和 JavaScript 对我有用。我的猜测是您的浏览器正在缓存您的旧 JavaScript 文件。尝试清除浏览器的缓存或不使用缓存刷新(通常是 ctrl-f5 或 shift-f5)。

于 2013-05-30T01:06:30.797 回答