我有一個表單在avidest.com/new。第一個字段是一個單選按鈕。在電子郵件字段下面是一個提示,應根據爲單選按鈕選擇哪個值進行更改。如果選擇僱主,則提示應該說「請使用您公司的電子郵件地址」。如果選擇了自由職業者,提示應該說「請使用您的學校電子郵件地址。」。出於某種原因,我的JavaScript代碼不起作用。我試圖用GetElementsByName替換getElementsByTagName,但這不起作用。這裏是我的html頁面的< head>部分寫功能:爲什麼GetElementsByTagName不以這種形式工作?
<script>
function topHint(){
if(document.getElementsByTagName('Type').value == "Freelancer"){
document.getElementById('employorlance').innerHTML = "Please use your school e-mail address.";
}
if(document.getElementsByTagName('Type').value == "Employer"){
document.getElementById('employorlance').innerHTML = "Please use your company e-mail address.";
}
}
</script>
我然後調用該函數在頁面加載時:
<body onload = "topHint()">
這裏是在表單元素頁碼:
<label id="item4_0_label">
<input type="radio" id="item4_0_radio" data-hint="" required value="Freelancer" name="Type" />
<span class="fb-fieldlabel" id="item4_0_span">Freelancer
</span>
</label>
<label id="item4_1_label">
<input type="radio" id="item4_1_radio" required value="Employer" name="Type" />
<span class="fb-fieldlabel" id="item4_1_span">Employer
</span>
</label>
<div class="fb-hint" id ="employorlance" style="color: rgb(136, 136, 136); font-weight: normal; font-style: normal; ">
Please use your school email address.
</div>
謝謝。
後從stakoverflow的建議,我嘗試以下,但它仍然不工作:
function topHint() {
var elems = document.getElementsByName("Type");
for (i in elems) {
if (elems[i].value == "Freelancer") {
document.getElementById('employorlance').innerHTML = "Please use your school e-mail address.";
}
if (elems[i].value == "Employer") {
document.getElementById('employorlance').innerHTML = "Please use your company e-mail address.";
}
}
}
的getElementsByTagName( '類型')返回值的集合,你需要更具體一點,它的標籤名稱。 – Toping 2013-03-07 20:56:19