jQuery.HCheckbox 单选框、复选框表单美化

本文介绍了一个基于jQuery的表单美化插件,通过简单的代码实现了单选框和多选框样式的自定义,并提供了获取表单值的方法。该插件能够帮助前端开发者快速提升表单界面的美观度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • jQuery.HCheckbox 基于jQuery的单选框、多选框表单美化的代码例子,将复选框变化成自己喜欢的风格,不再单调和呆板,示例并演示了如何取出表单的值,对WEB前端设计相当有帮助的一个例子。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title>河北礼品公司</title>
  <script type="text/javascript" src="http://www.codefans.net/ajaxjs/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
;(function($){
    $.fn.hcheckbox=function(options){
        $(':checkbox+label',this).each(function(){
            $(this).addClass('checkbox');
            if($(this).prev().is(':disabled')==false){
                if($(this).prev().is(':checked'))
                    $(this).addClass("checked");
            }else{
                $(this).addClass('disabled');
            }
        }).click(function(event){
                if(!$(this).prev().is(':checked')){
                    $(this).addClass("checked");
                    $(this).prev()[0].checked = true;
                }
                else{
                    $(this).removeClass('checked');            
                    $(this).prev()[0].checked = false;
                }
                event.stopPropagation();
            }
        ).prev().hide();
    }

    $.fn.hradio = function(options){
        var self = this;
        return $(':radio+label',this).each(function(){
            $(this).addClass('hRadio');
            if($(this).prev().is("checked"))
                $(this).addClass('hRadio_Checked');
        }).click(function(event){
            $(this).siblings().removeClass("hRadio_Checked");
            if(!$(this).prev().is(':checked')){
    $(this).addClass("hRadio_Checked");
                $(this).prev()[0].checked = true;
            }
               
            event.stopPropagation();
        })
        .prev().hide();
    }
})(jQuery)
</script>
<style>
label.checkbox {padding-left: 24px; cursor:pointer;background: url(http://www.codefans.net/jscss/demoimg/201105/checkbox.png) no-repeat left -21px; line-height:22px; height:21px; display:inline-block; } 
label.checked {background-position:left 100%;  } 
label.disabled{background-position:left 0px;}
.hRadio 
{
    padding-left: 22px; display: inline-block;
    background: transparent url(http://www.codefans.net/jscss/demoimg/201105/radio.png) no-repeat left top;
    height: 19px; line-height:20px;
    cursor:pointer;vertical-align:middle;
}
.hRadio_Checked { background-position: left bottom;}
</style>
  <script type="text/javascript">
$(function(){
    $('#chklist').hcheckbox();
    $('#radiolist').hradio();
    $('#btnOK').click(function(){
        var checkedValues = new Array();
        $('#chklist :checkbox').each(function(){
            if($(this).is(':checked'))
            {
                checkedValues.push($(this).val());
            }
        });

        alert(checkedValues.join(','));
        alert($('#radiolist :checked').val());
    })
});
  </script>
 </head>

 <body>
<div id="chklist" style="padding:10px; font-size:14px; ">
<input type="checkbox" value='1' /><label>aaaaaa</label>
<input type="checkbox" value='2' /><label>bbbbbb</label>
<input type="checkbox" value='3' /><label>ccccc</label>
<input type="checkbox" value='4' /><label>ddddd</label>
</div>

<div id="radiolist" style="padding:10px; font-size:14px; ">
<input name='r' type="radio" value='11' /><label>AAAAAA</label>
<input name='r' type="radio" value='21' /><label>BBBBBBBB</label>
<input name='r' type="radio" value='31' /><label>CCCCCC</label>
<input name='r' type="radio" value='41' /><label>DDDDDD</label>
</div>
<input type="button" value="确定" id="btnOK" />
 </body>
</html>

转载于:https://www.cnblogs.com/youtianxia/p/3864854.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值