How to change the default look of radio button, not functionality?

孤街浪徒 提交于 2019-12-23 22:18:45

问题


On a form I've two radio button Yes and No. I want to change the default look of radio buttons

to like this.

Edit: I would like to change the radio buttons into <a> anchor when JavaScript is enabled if it's not easy to change the look of default radio via css.


回答1:


Easiest way? Set the images as labels with the for attribute pointing to the radio buttons. Then set the radio buttons to display:none;

<input type="radio" name="radio[1]" /><label for="radio[1]"><img src="/yes.png"></label>
<input type="radio" name="radio[2]" /><label for="radio[2]"><img src="/no.png"></label>

p.s. This uses built in HTML functionality that works everywhere and doesn't require javascript. Use some CSS meta selectors (:hover etc...) to add animation.

UPDATE:

Just looping back after a long time and realised this could do with a little more explanation for beginners. When the for attribute is set on a label, clicking the label is functionally the same as clicking the element it's for attribute points to. For completeness, the for attribute should be set to the id of the form element.




回答2:


Rather than try to coerce the images to work with the <radio> tag, I would just put the images where you want them, bind the various behaviors to them using jQuery, and have them modify hidden input fields. I think it will be much easier that way.



来源:https://stackoverflow.com/questions/5120775/how-to-change-the-default-look-of-radio-button-not-functionality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!