2013-04-05 48 views
0

我是JavaScript新手,我一直在做基於HTML和JavaScript創建表單的大學作業。在這項任務中,我被要求根據是否在上一個下拉框中選擇了一個特定的選項來強制一個字段。選擇製作字段必選

  1. 如果「西悉尼大學學生」或「UWS工作人員」從較場「學號/員工數」下拉列表中選擇成爲強制性的,否則它不是一個必填字段

  2. 如果「西悉尼大學學生」或「UWS員工」比字段「機構/公司的選擇必須是自動填充與「西悉尼大學的」

下面是一段我一直在使用:

<b>Registration Category:</b> 

<select> 
<option value="UWS Student">UWS Student</option> 
<option value="Student at another institution">Student at another institution</option> 
<option value="UWS Academic">UWS Academic</option> 
<option value="Other UWS Staff">Other UWS Staff</option> 
<option value="Academic from another Institution">Academic from another  
Institution</option> 
<option value="Professional">Professional</option> 
<option value="Retired">Retired</option> 
</select> 

<b>Student Number/Staff Number:</b> <input type="text" name="StudentNumber/StaffNumber"> 
<br> 
<b>Institution/Company:</b> <input type="text" id="txtinstcomp" name="institutioncompany"> 

</fieldset> 

注:我也有前一個問題在「機構/公司」字段的功能只是爲了讓申請強制的,如果該事項在所有:

function validateText() 
{ 
var institutioncompany=document.getElementById('txtinstcomp'); 
if (institutioncompany.value=="") 
{ 
    alert("Institution/company must be filled out"); 
    return false; 
} 
} 

任何幫助將不勝感激因爲我很難掌握JavaScript

回答

1

您必須添加Id屬性才能選擇並輸入如下所示的文本字段。

<b>Registration Category:</b> 

<select id="cat" onchange="populateInstitution();"> 
    <option value="UWS Student">UWS Student</option> 
    <option value="Student at another institution"> 
      Student at another institution</option> 
    <option value="UWS Academic">UWS Academic</option> 
    <option value="Other UWS Staff">Other UWS Staff</option> 
    <option value="Academic from another Institution"> 
      Academic from another Institution</option> 
    <option value="Professional">Professional</option> 
    <option value="Retired">Retired</option> 
</select> 
<br> 
<b>Student Number/Staff Number:</b> 
<input type="text" id="stNumber" name="StudentNumber/StaffNumber"> 
<br> 
<b>Institution/Company:</b> 
<input type="text" id="txtinstcomp" name="institutioncompany"> 
<br> 
<input type="button" value="submit" onclick="validateText()" /> 

,並有像更改事件「populateInstitution」 javascript功能上選擇字段

function validateText() { 
    var institutioncompany = document.getElementById('txtinstcomp').value; 
    if (institutioncompany == "") { 
     alert("Institution/company must be filled out"); 
     return false; 
    } 
    var cat = document.getElementById('cat').value; 
    if (cat == "UWS Student" || cat == "Other UWS Staff") { 
     if (document.getElementById("stNumber").value == "") { 
      alert('StudentNumber/StaffNumber is mandatory'); 
      return; 
     } 

    } 
} 

function populateInstitution() { 
    var cat = document.getElementById('cat').value; 
    if (cat == "UWS Student" || cat == "Other UWS Staff") { 
     document.getElementById('txtinstcomp').value='University of Western Sydney'; 
    }else{ 
     document.getElementById('txtinstcomp').value=''; 
    } 
} 
+0

完美地工作,非常感謝! – bigsenator 2013-04-05 08:45:14