UPDATE:请参阅bryanbraun's answer。他好多了。
在HTML,<input type="image">
仅仅是一个图像作为它的脸提交按钮。我不认为这就是你想要的。您可能想要一个实际图像列表,点击时根据“数据值”属性设置一个隐藏表单值。所以,你的HTML应该是这个样子:
<form id="select-form">
<img src="image.jpg" data-value="image_value">
<img src="image2.jpg" data-value="image_value2">
<input type="hidden" id="image-value" name="selected_image" value="">
</form>
然后,在JavaScript,你既想突出图像,并设置隐藏的价值,就像这样:
$('#select-form img').click(function() {
// Set the form value
$('#image-value').val($(this).attr('data-value'));
// Unhighlight all the images
$('#select-form img').removeClass('highlighted');
// Highlight the newly selected image
$(this).addClass('highlighted');
});
最后,设置一些风格在你的CSS文件中的亮点类:
img.highlighted {
border: 2px solid blue;
}
作为目前被接受的答案(我在5年前写的!)的人,我同意这是一个更好的解决方案,并且应该被接受回答。 – 2015-05-22 20:19:20
因为我无法控制我的答案的可接受性,所以我用一个大胆的注释更新了我的答案,指出了你的答案。 – 2015-05-22 20:20:52
(我甚至没有注意到这个答案,直到现在) – 2015-05-22 20:21:19