更大的无线电选择范围

发布于 2024-08-20 06:49:33 字数 251 浏览 6 评论 0原文

<span>
   <img src="img/icon.png" alt="" />
   <label><input type="radio" name="" /> Label here</label>
</span>

我希望整个 都是可点击的,而不仅仅是无线电输入。我如何使用 jQuery 做到这一点?

<span>
   <img src="img/icon.png" alt="" />
   <label><input type="radio" name="" /> Label here</label>
</span>

I want the whole <span> to be clickable, not just the radio input. How do I do it with jQuery?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

油饼 2024-08-27 06:49:33

好吧,最简单的解决方案是将所有内容包装在 标记内,如下所示:

<label for="foo">
    <img src="img/icon.png" alt="" />
    <input type="radio" name="" id="foo" />
</label>

当您为标签指定 for 属性时,以及相同的 id 到字段中,标签变得可点击并将激活相应的输入。

但是,如果您出于某种原因需要在 jQuery 中执行此操作,那么这应该可行:

$('span').click(function() {
    $(this).find('input').click();
    return false;
});

Well, the easiest solution would be to wrap everything inside the <label> tag, like this:

<label for="foo">
    <img src="img/icon.png" alt="" />
    <input type="radio" name="" id="foo" />
</label>

When you specify an for attribute to label, and the same id to the field, the label becomes clickable and will activate the corresponding input.

But, if you for some reason need to do it in jQuery, this should work:

$('span').click(function() {
    $(this).find('input').click();
    return false;
});
故事还在继续 2024-08-27 06:49:33

您可以在没有 jQuery 的情况下完成此操作,只需将 改为 即可:

<label for="some-id">
   <img src="img/icon.png" alt="" />
   <input type="radio" name="" id="some-id" /> Label here
</label>

You could do it without jQuery by just making the <span> the <label> instead:

<label for="some-id">
   <img src="img/icon.png" alt="" />
   <input type="radio" name="" id="some-id" /> Label here
</label>
寂寞陪衬 2024-08-27 06:49:33

这是一个更好的方法。

$('span').click(function() {
    $(this).find('input').attr({checked:"checked"});
});

请记住,您正在向所有范围添加点击事件。更好的办法是开设一个关于跨度的课程并参考它。

$('.myClickySpan')...

<span class='myClickySpan'>...

This is a better way.

$('span').click(function() {
    $(this).find('input').attr({checked:"checked"});
});

Just keep in mind that you are adding a click event to all spans. Better would be to have a class on the span and reference that.

$('.myClickySpan')...

<span class='myClickySpan'>...
淑女气质 2024-08-27 06:49:33

您似乎没有使用 label< 的 for 属性/a>.也许这样做会对你有所帮助

<input type="radio" name="r" id="r" />
<label for="r">
   <img src="img/icon.png" alt="" />
   Label here
</label>

It looks like you're not using the for attribute of the label. Maybe doing so will help you

<input type="radio" name="r" id="r" />
<label for="r">
   <img src="img/icon.png" alt="" />
   Label here
</label>
浅忆 2024-08-27 06:49:33
$("span").click(function(){
  var radio = $(this).find('input:radio');
  //do whatever with the radio button
});

我相信应该这样做。

$("span").click(function(){
  var radio = $(this).find('input:radio');
  //do whatever with the radio button
});

I believe should do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文