新手的屬性'元素',即時試圖找出爲什麼我得到「無法讀取未定義的屬性'元素'。簡單的表單驗證幫助「無法讀取未定義的」
的javascript:
function conf()
{
for (i=0; i<5; i++)
{
box = document.project.elements[i];
if (!box.value)
{
alert('You haven\'t filled in ' + box.name + '!');
return false
}
}
return true;
}
HTML:
<form id="project" onsubmit="return conf()" action="#">
<fieldset>
<legend>Contact Details</legend>
<label for="txtname">Your Name:</label> <input type="text" class="text" name="txtname" id="1" /><br/>
<label for="txtemail">Email:</label> <input type="text" class="text" name="txtemail" id="2"/><br/>
<label for="txtartist">Artist:</label> <input type="text" class="text" name="txtartist" id="3"/><br/>
<label for="txtalbum">Song/Album:</label> <input type="text" class="text" name="txtalbum" id="4"/><br/>
<label for="txtcomments">Comments:</label> <textarea class="ta" name="txtcomments" id="txtcomments" cols="30" rows="20"></textarea><br/>
<input type="submit" class="buttons" id="btnSubmit" name="btnsubmit" value="Send Enquiry"/>
</fieldset>
</form>
對不起,第7行: box = document.project.elements [i]; – user3720610