最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Turn radio buttons into links by jQuery - Stack Overflow

programmeradmin0浏览0评论

I don't know what the specific name to this kind of form controller is (if there even is one), but basically I want to change radio elements like so;

<input type="radio" name="size" value="S" id="size_S" />
  <label for="size_S">S</label>
<input type="radio" name="size" value="M" id="size_M" />
  <label for="size_M">M</label>
<input type="radio" name="size" value="L" id="size_L" />
  <label for="size_L">L</label>

...into stylized links, the checked of which has a special class. For an example, I'd list the size selection element on Macys.

How can I turn the radios into these kinds of box links? Is there a library or plug-in that already does this?

I can modify the DOM, but the elements need to be radio buttons for the convenience of non-JS users.

I don't know what the specific name to this kind of form controller is (if there even is one), but basically I want to change radio elements like so;

<input type="radio" name="size" value="S" id="size_S" />
  <label for="size_S">S</label>
<input type="radio" name="size" value="M" id="size_M" />
  <label for="size_M">M</label>
<input type="radio" name="size" value="L" id="size_L" />
  <label for="size_L">L</label>

...into stylized links, the checked of which has a special class. For an example, I'd list the size selection element on Macys..

How can I turn the radios into these kinds of box links? Is there a library or plug-in that already does this?

I can modify the DOM, but the elements need to be radio buttons for the convenience of non-JS users.

Share Improve this question asked May 28, 2012 at 1:03 Emphram StavangerEmphram Stavanger 4,2249 gold badges38 silver badges66 bronze badges 1
  • What have you tried? – j08691 Commented May 28, 2012 at 1:11
Add a ment  | 

2 Answers 2

Reset to default 6

You can pretty much reuse the standard form elements. With js enabled, hide the radio buttons, style the labels, and bind a click event to add the active/selected class. Clicking a label will still select the underlying radiobutton.

Example jsFiddle: http://jsfiddle/Y6BzY/

JS

$('input[type="radio"], label').addClass('js');

$('label').on('click', function() {
    $('label').removeClass('active');
    $(this).addClass('active');
});

CSS

input[type="radio"].js {
    display: none;
}

label.js {
    display: block;
    float: left;
    margin-right: 5px;
    border: 1px solid black;
    padding: 4px;
    cursor: pointer;
}

label.js.active {
    border: 1px solid blue;
    color: blue;
}

Ephram,

I visited the link you have provided and all I can see there are spans with a number in them for size selection. Did you mean those, or like those?

About you're question: I understand that the reason to show links instead of boxes would be the absence of js support. Have you considered using a tag with hardcoded radio buttons in them and spans, like in your example, outside the noscript tags?

In any case, I don't really see the need to manipulate the DOM for this to be honest.

发布评论

评论列表(0)

  1. 暂无评论