在表单中突出显示选定的图像 - 使用 Jquery?
我在谷歌上搜索了答案,但发现什么也没有,所以即使是一个显示如何执行以下操作的页面的链接也将不胜感激。
基本上我有一个只有图像的表单,
<form>
<input type="image" src="image.jpg" value="image_value">
<input type="image" src="image2.jpg" value="image_value2">
</form>
我希望能够以某种方式突出显示用户选择的图像。当用户单击图像 1 时,即使只是图像 1 周围的轮廓也是完美的。
我已经在这个项目中使用了 Jquery,所以如果有一个 jquery 解决方案,那将是最方便的。
I've searched Google for the answer and have found nada so even a link to a page showing how to do the following will be much appreciated.
Basically I have a form with nothing but images
<form>
<input type="image" src="image.jpg" value="image_value">
<input type="image" src="image2.jpg" value="image_value2">
</form>
I want to be be able to highlight in some way the image the user has selected. Even just an outline around image 1 when the user clicks image 1 would be perfect.
I already am using Jquery on this project so if there is a jquery solution it would be the handiest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一种可访问的方法是将单选按钮标签的样式设置为像图像选择一样:
然后是CSS 。正如您所看到的,无线电本身被隐藏,不透明度为 0:
这是一个实际示例:http://jsfiddle。 net/RH98R/
这还有一个额外的好处,就是不依赖 jQuery(甚至是 javascript)!
An accessible approach would be to style radio buttons labels to behave like an image select:
And then the CSS. As you can see, the radios themselves are concealed with an opacity of 0:
Here's an example in action: http://jsfiddle.net/RH98R/
This has the added benefit of not having a dependency on jQuery (or even javascript for that matter)!
更新:请参阅bryanbraun 的回答。他的要好得多。
在 HTML 中,
只是一个以图像为表面的提交按钮。我不认为这就是你想要的。您可能需要一个实际图像的列表,单击该列表时,会根据“数据值”属性设置隐藏的表单值。所以你的 HTML 应该看起来像这样:
然后在你的 javascript 中,你想要突出显示图像并设置隐藏值,如下所示:
最后,在 CSS 文件中为突出显示类设置一些样式:
UPDATE: Please see bryanbraun's answer. His is much better.
In HTML,
<input type="image">
is just a submit button with an image as its face. I don't think that's what you want. You probably want a list of actual images, that when clicked, sets a hidden form value according to a "data-value" attribute. So your HTML should look something like this:Then in your javascript, you want to both highlight the image and set the hidden value, like this:
Finally, set some styles for the highlight class in your CSS file:
像下面这样的东西应该可以工作,你可以创建样式 CSS 类并更容易地更改样式。
Something like the following should work, you could make the styles CSS classes and change the style more easily though.
类似于
CSS:
javascript:
something like
CSS:
javascript:
以下内容将为每个图像输入提供 1 像素边框和 10 像素点击时的填充,但理想情况下,您应该为它们提供一个类来定位它们,例如:
$(".imgs2higlight").bind()
The following will give every image input a 1px border and 10px padding when clicked, but ideally you should give them a class to target them with like:
$(".imgs2higlight").bind()