首页 > 解决方案 > 确定 On/Off 开关是 On 还是 Off

问题描述

这是我的 html 开关:

            $('#myonoffswitch').click(function () {
                alert($('#myonoffswitch').val());
            });
.onoffswitch {
    position: relative;
    width: 77px;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

.onoffswitch-checkbox {
    display: none;
}

.onoffswitch-label {
    display: block;
    overflow: hidden;
    cursor: pointer;
    border: 2px solid #8A8585;
    border-radius: 11px;
}

.onoffswitch-inner {
    display: block;
    width: 200%;
    margin-left: -100%;
    transition: margin 0.3s ease-in 0s;
}

    .onoffswitch-inner:before, .onoffswitch-inner:after {
        display: block;
        float: left;
        width: 50%;
        height: 26px;
        padding: 0;
        line-height: 26px;
        font-size: 16px;
        color: white;
        font-family: Trebuchet, Arial, sans-serif;
        font-weight: bold;
        box-sizing: border-box;
    }

    .onoffswitch-inner:before {
        content: "ON";
        padding-left: 5px;
        background-color: #282828;
        color: #0C7A22;
    }

    .onoffswitch-inner:after {
        content: "OFF";
        padding-right: 5px;
        background-color: #282828;
        color: #FF0000;
        text-align: right;
    }

.onoffswitch-switch {
    display: block;
    width: 34px;
    margin: -4px;
    background: #282828;
    position: absolute;
    top: 0;
    bottom: 0;
    right: 47px;
    border: 2px solid #8A8585;
    border-radius: 11px;
    transition: all 0.3s ease-in 0s;
}

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px;
    background-color: #282828;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="onoffswitch">
                        <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" />
                        <label class="onoffswitch-label" for="myonoffswitch">
                            <span class="onoffswitch-inner"></span>
                            <span class="onoffswitch-switch"></span>
                        </label>
                    </div>

为什么总是alert($('#myonoffswitch').val());显示on
如何确定 On/Off 开关是 On 还是 Off

标签: htmljquery

解决方案


.is(":checked")代替.val(): _

$('#myonoffswitch').click(function () {
                alert($('#myonoffswitch').is(":checked"));
            });
.onoffswitch {
    position: relative;
    width: 77px;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

.onoffswitch-checkbox {
    display: none;
}

.onoffswitch-label {
    display: block;
    overflow: hidden;
    cursor: pointer;
    border: 2px solid #8A8585;
    border-radius: 11px;
}

.onoffswitch-inner {
    display: block;
    width: 200%;
    margin-left: -100%;
    transition: margin 0.3s ease-in 0s;
}

    .onoffswitch-inner:before, .onoffswitch-inner:after {
        display: block;
        float: left;
        width: 50%;
        height: 26px;
        padding: 0;
        line-height: 26px;
        font-size: 16px;
        color: white;
        font-family: Trebuchet, Arial, sans-serif;
        font-weight: bold;
        box-sizing: border-box;
    }

    .onoffswitch-inner:before {
        content: "ON";
        padding-left: 5px;
        background-color: #282828;
        color: #0C7A22;
    }

    .onoffswitch-inner:after {
        content: "OFF";
        padding-right: 5px;
        background-color: #282828;
        color: #FF0000;
        text-align: right;
    }

.onoffswitch-switch {
    display: block;
    width: 34px;
    margin: -4px;
    background: #282828;
    position: absolute;
    top: 0;
    bottom: 0;
    right: 47px;
    border: 2px solid #8A8585;
    border-radius: 11px;
    transition: all 0.3s ease-in 0s;
}

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px;
    background-color: #282828;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="onoffswitch">
                        <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" />
                        <label class="onoffswitch-label" for="myonoffswitch">
                            <span class="onoffswitch-inner"></span>
                            <span class="onoffswitch-switch"></span>
                        </label>
                    </div>


推荐阅读