-2
所以我一直無法理解爲什麼我的「document.getElementbyID()」函數正在打破我的外部腳本。我的腳本和HTML文件是這樣的:getElementbyId爲一個div中的項目
function validateRegCat(obj)
{
\t setRegCat(obj);
\t var invalid = false;
\t if(obj.value == "Invalid")
\t {
\t \t alert("Please select a Registration category")
\t \t invalid = true;
\t }
\t return invalid;
\t
}
function setRegCat(obj)
{
\t if(obj.value == "UWSStudent")
\t {
\t \t document.getElementbyId("Institution").value = "The University of Western Sydney";
\t \t document.getElementbyId("Institution").readOnly = true;
\t }
}
<body>
<form class="Application" action"prac1task3Form.asp" method="post" onsubmit="return finalValidate(this);" >
\t \t \t <div class="PersonalDetails">
\t \t \t \t <fieldset>
<legend>
\t \t \t \t <h3>Personal Details</h3>
\t \t \t \t </legend>
\t \t \t \t <!--Kept in a list to keep the form looking neat and organised-->
\t \t \t \t <ul>
<li>
<!-- Create a selection box for the user to input their Registration Category-->
<label for="RegCat"><strong>Registration Category:</strong><sup>*</sup></label>
<select name="RegCat" size="1" onblur="validateRegCat(this); setRegCat(this)" required>
<option value = "Invalid">--Choose a registration category--</option>
<option value = "UWSStudent">UWS Student</option>
<option value = "OtherStudent">Student at another Institution</option>
<option value = "UWSAcademic">UWS Academic</option>
<option value = "UWSStaff">UWS Staff</option>
<option value = "OtherAcademic">Academic from another Institution</option>
<option value = "PublicMember">Member of the public</option>
<option value = "Retired">Retired</option>
</select>
</li>
<!-- Input Institution. Sets automatically to Uws and readonly if certain registration categorys are selected-->
\t \t \t \t <li>
\t \t \t \t \t <label for="Institution"><strong>Institution of learning/work:</strong> </label>
<input type="text" name="Institution" id="Institution" size="30" maxlength="30"/>
\t \t \t \t \t </li> \t
</ul>
</fieldset>
\t \t \t </div> \t \t
</form>
</body>
有一些Uncaught ReferenceError:validateRegCat沒有定義,首先解決這個問題。 – stanze
下次您應該檢查您的JavaScript控制檯是否有錯誤。具體錯誤是*「Uncaught TypeError:undefined不是函數」*。這是由於在下面的答案中提到的錯字。 – Phil