我有一個註冊頁面的表單。該表單包含一個根據情況更改其src的圖像。 在提交表單後激活的腳本中,如果圖像具有特定的src,我希望表單調用alert,所以我需要一種檢索和比較值的方法。獲取img.src值(路徑名)
HTML:
<form action="register_submit.php" method="post" name="mainform" enctype="multipart/form-data" onSubmit="return checkForm(this);return false;">
JS:
function checkForm(f)
{if ([image src value] == "pictures/apic.png")
{
alert("error picture is apic");
return false;
}
else
{
f.submit();
return false;
}
}
這裏是相對碼全:
<script type="text/javascript">
function checkForm(f)
{if ([image src value] == "pictures/apic.png")
{
alert("error picture is apic");
return false;
}
else
{
f.submit();
return false;
}
}
</script>
<form action="register_submit.php" method="post" name="mainform" enctype="multipart/form-data" onSubmit="return checkForm(this);return false;">
<div class="required">
<label for="first_name">*First Name:</label>
<input type="text" name="first_name" id="first_name" class="inputText" onkeyup="checkFName(this.value);" onblur="checkFName(this.value);" maxlength="20" size="10" value="" />
<img id="FName_Status" name="FName_Status" src="/pictures/bad.png" style="margin-left:5px; position:absolute;" alt="FName_Status" />
</div>
(OTHER OBJECTS)
</form>
<input type="submit" name="sub" class="inputSubmit" value="Submit »"/>
在哪裏指定img元素,因爲頁面上有許多元素。例如,如果
'f.getElementsByTagName('img')[0]' refers to the first image element inside your 'f' form –
is there any way to state a range (e.g. all elements) or do this by image id/name instead. –