纯css美化下拉框、复选框以及单选框样式并用jquery获取到其被选中的val

具体样式如图所示:

注:获取val值时记得要先引入jquery库奥。

1.下拉框

css部分

#cargo_type_id{
                font-size: 13px;
                border: solid 1px #b5b5b5;
                appearance: none;
                -moz-appearance: none;
                -webkit-appearance: none;
                padding: 3px 10px;
                padding-right: 30px;
                background: url(xiala.png) no-repeat scroll right center transparent;
                background-size: 11.5px auto;
                background-position: right 10px top 9px;
                color: #333;
            }

html

<select id="cargo_type_id">
          	<option value="">请选择</option>
          	<option value="1">瓶</option>
          	<option value="2">袋</option>
          	<option value="3">包</option>
          	<option value="4">份</option>
        </select>
        <button  οnclick="opval();" >点此输出被选中option的val</button><br />

js

function opval(){
		console.log($("#cargo_type_id").find("option:selected").val())
	}

2.复选框

css

input[type="checkbox"]{
				-webkit-appearance:none;
				outline: none;
			}
			input.check{
				background:url(xuanzhongqian.png) no-repeat center left;
				background-size:20px 20px;
				width:20px;
				height:35px;
			}
			input.check:checked{
				background:url(xuanzhonghou.png) no-repeat center left;
				background-size:20px 20px;
			}

html

<input type="checkbox" class="check" value="1" >
        <input type="checkbox" class="check" value="2" checked="checked" >
        <input type="checkbox" class="check" value="3" >
        <input type="checkbox" class="check" value="4" ><br />
        <button  οnclick="cbval();" >点此输出被选中checkbox的val</button><br /><br />

js

function cbval(){
		$('body').find(".check").each(function() { 
        if ($(this).is(":checked")) { 
          console.log($(this).val())
        }
      });
	}

3.单选框(不得不说 这个好麻烦 )

css

.address input {
			    position: absolute;
			    display: none;
			}
			.address input + label {
			    position: relative;
			    display: block;
			    padding-left: 20px;
			    cursor: pointer;
			    vertical-align: middle;
			    margin-left: 10px;
			    margin-top: -18px;
			    text-align: left;
			}
			.address input + label:first-of-type {
			    margin-top: 0;
			}
			.address input + label:before {
			    position: absolute;
			    top: 0;
			    left: 0;
			    display: inline-block;
			    width: 12px;
			    height: 12px;
			    content: '';
			    border: 1px solid #c0c0c0;
			    border-radius: 50%;
			    margin-top: 3px;
			}
			.address input + label:after {
			    position: absolute;
			    display: none;
			    content: '';
			}
			.address input:checked + label:after {
			    display: block;
			}
			.address input:checked + label:before {
			    border: 1px solid #84B241;
			}
			.address input + label:after {
			    top: 6px;
			    left: 3px;
			    width: 8px;
			    height: 8px;
			    border-radius: 50%;
			    background: #84B241;
			}

html

<div class="address">
        	<input type="radio" name="addr" id="addr1" value="Beijing" checked="checked" />
			<label for="addr1">BEIJING</label><br />
			<input type="radio" name="addr" id="addr2" value="Harbin" />
			<label for="addr2">HARBIN</label><br />
			<input type="radio" name="addr" id="addr3" value="Qingdao" />
			<label for="addr3">QINGDAO</label><br />
			<button  οnclick="radval();" >点此输出被选中radio的val</button><br />
        </div>

js

function radval(){
		console.log($("input[name='addr']:checked").val());
	}

转载于:https://www.cnblogs.com/lilelile/p/7743103.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值