2015-04-07 45 views
-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>
我剪的HTML下降到僅相關部分。我想讓腳本執行的操作是將「Institution」文本輸入的值更改爲只讀,並設置顯示的文本。但是我發現「document.getElementbyId(」Institution「)」打破了腳本。該腳本在此之前運行良好,當我將這些行更改爲無意義的事件(如警報)時,腳本功能和返回應該如此。

+0

有一些Uncaught ReferenceError:validateRegCat沒有定義,首先解決這個問題。 – stanze

+0

下次您應該檢查您的JavaScript控制檯是否有錯誤。具體錯誤是*「Uncaught TypeError:undefined不是函數」*。這是由於在下面的答案中提到的錯字。 – Phil

回答

相關問題