在表单中突出显示选定的图像 - 使用 Jquery?

发布于 2024-10-02 08:17:37 字数 382 浏览 4 评论 0原文

我在谷歌上搜索了答案,但发现什么也没有,所以即使是一个显示如何执行以下操作的页面的链接也将不胜感激。

基本上我有一个只有图像的表单,

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

早乙女 2024-10-09 08:17:37

一种可访问的方法是将单选按钮标签的样式设置为像图像选择一样:

<form action="#" method="post">
    <input type="radio" class="radio" name="example" id="ex1" value="ex1" checked />
    <label for="ex1" class="label">Example 1</label>
    <input type="radio" class="radio" name="example" id="ex2" value="ex2" />
    <label for="ex2" class="label">Example 2</label>
</form>

然后是CSS 。正如您所看到的,无线电本身被隐藏,不透明度为 0:

.radio {
    opacity: 0;
    position: absolute;
}
.label {
    background: url(http://i.imgur.com/mW6xr2I.png);
    text-indent: -999em;
    border: 10px solid white;
    width: 126px;
    height: 126px;
    display: block;
    float: left;
    margin: 10px;
}
input[type="radio"]:checked + label {
    border: 10px solid orange;
}

这是一个实际示例:http://jsfiddle。 net/RH98R/

这还有一个额外的好处,就是不依赖 jQuery(甚至是 javascript)!

An accessible approach would be to style radio buttons labels to behave like an image select:

<form action="#" method="post">
    <input type="radio" class="radio" name="example" id="ex1" value="ex1" checked />
    <label for="ex1" class="label">Example 1</label>
    <input type="radio" class="radio" name="example" id="ex2" value="ex2" />
    <label for="ex2" class="label">Example 2</label>
</form>

And then the CSS. As you can see, the radios themselves are concealed with an opacity of 0:

.radio {
    opacity: 0;
    position: absolute;
}
.label {
    background: url(http://i.imgur.com/mW6xr2I.png);
    text-indent: -999em;
    border: 10px solid white;
    width: 126px;
    height: 126px;
    display: block;
    float: left;
    margin: 10px;
}
input[type="radio"]:checked + label {
    border: 10px solid orange;
}

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)!

贱贱哒 2024-10-09 08:17:37

更新:请参阅bryanbraun 的回答。他的要好得多。


在 HTML 中, 只是一个以图像为表面的提交按钮。我不认为这就是你想要的。您可能需要一个实际图像的列表,单击该列表时,会根据“数据值”属性设置隐藏的表单值。所以你的 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;
}

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:

<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>

Then in your javascript, you want to both highlight the image and set the hidden value, like this:

$('#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');
});

Finally, set some styles for the highlight class in your CSS file:

img.highlighted {
    border: 2px solid blue;
}
止于盛夏 2024-10-09 08:17:37

像下面这样的东西应该可以工作,你可以创建样式 CSS 类并更容易地更改样式。

$("input[type='image']")
    .focus(function() { $(this).css("border","solid 1px #f00"); })
    .blur(function() { $(this).css("border","0"); });

Something like the following should work, you could make the styles CSS classes and change the style more easily though.

$("input[type='image']")
    .focus(function() { $(this).css("border","solid 1px #f00"); })
    .blur(function() { $(this).css("border","0"); });
执着的年纪 2024-10-09 08:17:37

类似于

CSS:

.selected { border: 1px solid black; }

javascript:

$(function() {
     $("input[type=image]").click(function(){ $(this).addClass('selected'); });
});

something like

CSS:

.selected { border: 1px solid black; }

javascript:

$(function() {
     $("input[type=image]").click(function(){ $(this).addClass('selected'); });
});
谁人与我共长歌 2024-10-09 08:17:37

以下内容将为每个图像输入提供 1 像素边框和 10 像素点击时的填充,但理想情况下,您应该为它们提供一个类来定位它们,例如: $(".imgs2higlight").bind()

  $(document).ready(function() {
            $("input[type='image']").bind('click',function(e) {
e.preventDefault();
            $(this).css({padding : '10px', border: "1px solid blue"});
            })
            })

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()

  $(document).ready(function() {
            $("input[type='image']").bind('click',function(e) {
e.preventDefault();
            $(this).css({padding : '10px', border: "1px solid blue"});
            })
            })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文