首页 > 网站 > WEB开发 > 正文

自定义表单样式之checkbox和radio

2024-04-27 14:25:25
字体:
来源:转载
供稿:网友
自定义表单样式之checkbox和radio

1,起因

最近在工作中要实现自定义式的radio样式,而我们通常使用的时默认的样式,因为自己实在想不到解决的方法,于是开始搜索,最终看到了不错的解决办法,可以完美解决我们遇到的问题。

2,原理

大家都知道在写结构的时候,radio或checkbox都会跟随label一起使用,label的for属性值和input的id值相同的情况下,点击label就可以选中input,这里正是利用label来覆盖我们的input默认样式,通过给label添加背景图片(美化的checkbox或radio),也就是在点击的过程中,我们是看不到默认的input的(给input设置z-index:-1),而点击的是label,通过不同的事件,加载不同的背景图片(这里是改变背景图片的位置)

3,设置美化checkbox或radio的默认样式

(1)页面结构

<form class="form" method="post"><fieldset><legend>Which genres do you like?</legend><input type="checkbox" value="action" id="check-1" name="genre"><label for="check-1" class="">Action / Adventure</label><input type="checkbox" value="comedy" id="check-2" name="genre"><label for="check-2" class="">Comedy</label><input type="checkbox" value="epic" id="check-3" name="genre"><label for="check-3" class="">Epic / Historical</label><input type="checkbox" value="science" id="check-4" name="genre"><label for="check-4" class="">Science Fiction</label><input type="checkbox" value="romance" id="check-5" name="genre"><label for="check-5" class="">Romance</label><input type="checkbox" value="western" id="check-6" name="genre"><label for="check-6" class="">Western</label></fieldset><fieldset><legend>Caddyshack is the greatest movie of all time, right?</legend><input type="radio" value="1" id="radio-1" name="opinions"><label for="radio-1" class="">Totally</label><input type="radio" value="1" id="radio-2" name="opinions"><label for="radio-2" class="">You must be kidding</label><input type="radio" value="1" id="radio-3" name="opinions"><label for="radio-3" class="">What's Caddyshack?</label></fieldset></form>

(2)jquerycode(前提必须引入jquery库)

jQuery.fn.customInput = function(){$(this).each(function(i){if($(this).is('[type=checkbox],[type=radio]')){var input = $(this);//get the associated label using the input's idvar label = $('label[for='+input.attr('id')+']');//get type,for classname suffixvar inputType = (input.is('[type=checkbox]')) ? 'checkbox' : 'radio';//wrap the input + label in a div$('<div class="custom-'+ inputType +'"></div>').insertBefore(input).append(input,label);//find all inputs in this set using the shared name attributevar allInputs = $('input[name='+input.attr('name')+']');//necessary for browsers that don't support the :hover pseudo class on labelslabel.hover(function(){$(this).addClass('hover');if(inputType == 'checkbox' && input.is(':checked')) {$(this).addClass('checkedHover');}},function(){$(this).removeClass('hover checkedHover');});//bind custom event, trigger it, bind click,focus,blur eventsinput.bind('updateState',function(){if(input.is(':checked')){if(input.is(':radio')){allInputs.each(function(){$('label[for='+$(this).attr('id')+']').removeClass('checked');});};label.addClass('checked');} else {label.removeClass('checked checkedHover checkedFocus');}}).trigger('updateState').click(function(){$(this).trigger('updateState');}).focus(function(){label.addClass('focus');if(inputType == 'checkbox' && input.is(':checked')) {$(this).addClass('checkedFocus');}}).blur(function(){label.removeClass('focus checkedFocus');});}});}

引入jquery库,再引入上面的代码后,就可以执行下面的代码

$('input').customInput();

(3)生成的外层div

如果你的代码结构是label和input成对写的话,那么在它们的外层就会生成一个div,如图

ipz3w30qIDcmF8HXYAbIPDpU78VZO3Oa/HcsJ2Qe+IAAAxwVGRP/78YfvX/3n2+3X/3r54p8vPR/54tO33r0fzREhP7pN/NchfMrpBMPyXwSgQAAAjgjs9fevvvt28++vv3r51ZcvvvxsvXq++uRpnP82grvklwtijBgjXvKf5MSMDTz1X+VMcOZu0VWQU8bOGTvPxMZMNBdwtSNqXpOLS5XCSlZ2UnQWxs4Zu4z6fKqVZkNVxZJXLRZZcY1K0ZozFrhG5EQ5ZYyyjFhGPKsKb6ydJHFBGQvY0SzETVH8l1PGiGWUR8qxWmj0lkqvp8ux344AAACkgm03L5mN508/ivKf7/zllJXnfNxcPG3HQ3Aq1vXY6lmVbFOsueDmC17Qnr5mI4TjkbgpSfyXi5LAtAulFdApOQm/9bbLlYtKbZ5RTiQ5ibz4NxckcuKsYk3FUsHaCwIj4txuT2nNQK7yD6GbZJZT1uW0UDdbd1IN4AICAHAsYGrD84t/LD/72+O/P/vT8vHDJ/M/tNj/lJZ/EuM/ksS4tfa6iDkW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