我正在尝试更改表单内的一行文本;更具体地说,我希望文本在某些条件下发生变化。条件是检查用户何时单击按钮。在这种情况下,我希望在用户将输入字段留空时更改文本。此代码包括我正在使用的整个表单和脚本。我遇到的问题是单击按钮时文本似乎会发生变化,但只有一秒钟或只要警报消息出现。之后,它又回到原来的文本。我脚本中正在访问的行是第 5 行代码。
<div id="userRegistration"> <!-- Registration Form Shell -->
<form name="registration">
<div id="leftSideForm"> <!-- Left side of the Registration Form -->
<span style="color:red">*</span>
Enter a Username: <input type="text" name="userName" /> <br />
<b id="yy">test </b> <br />
<span style="color:red">*</span>
Enter Password: <input type="password" name="userPassword" /><br />
<span style="color:red">*</span>
Confirm Password: <input type="password" name="cUserPassword" /><br /> <br /> <br />
<span style="color:red">*</span>
First name: <input type="text" name="firstName" /><br />
Middle initial: <input type="text" name="middleName" /><br />
<span style="color:red">*</span>
Last name: <input type="text" name="lastName" /><br /> <br />
</div> <!-- end of Left sideform -->
<div id="rightSideForm">
<span style="color:red">*</span>
Email Address: <input type="text" name="userEmail" /><br />
<span style="color:red">*</span>
Confirm Email: <input type="text" name="confirmUserEmail" /><br /> <br /> <br /> <br />
<button type="submit" onclick="validateUsername()" >Register</button>
</div> <!-- end of right sideform -->
</form>
</div> <!-- End of registration -->
<script type="text/javascript">
function validateUsername()
{
var x=document.forms["registration"]["userName"].value;
if (x == null || x== "")
{
document.getElementById("yy").innerHTML = "WHY WON'T YOU WORK GRRRR";
alert("First name must be filled out");
}
}
</script>