也许你可以给每个图像一个 id(并传递一个对应于 changeColor 函数的数字,以分配给我们将用于提交表单的变量):
<img id="img1" src="inventory_images/pic1.jpg" onclick="changeColor(this, 1);" />
<img id="img2" src="inventory_images/pic2.jpg" onclick="changeColor(this, 2);" />
<img id="img3" src="inventory_images/pic3.jpg" onclick="changeColor(this, 3);" />
然后让它每次标记图像时,在你这样做之前,取消标记所有图像:
<script>
markedImage = 0; // This variable used for the submit;
function unmarkAll() {
document.getElementById("img1").style.borderColor = "#000000"; // Assuming black is the default border color, you might want to change it.
document.getElementById("img2").style.borderColor = "#000000";
document.getElementById("img3").style.borderColor = "#000000";
}
function changeColor(obj, markedId) {
unmarkAll();
obj.style.borderColor ="#00FF00";
markedImage = markedId;
}
</script>
至于提交表单,我会使用隐藏的表单字段,并将其值设置为 onsubmit 事件中标记图像的编号。首先创建要提交的 HTML 表单:
<form action="submitPage.html" onsubmit="submit();">
<input type="hidden" id="markedImage" name="markedImage"></input>
<input type="submit" value="Submit"></input>
</form>
submit() 函数的 javascript 将是这样的:
<script>
function submit()
{
document.getElementById("markedImage").value = markedImage;
}
</script>