2016-03-02 35 views
1

我需要使该切换效果在任何单个复选框上工作。例如,我会检查第二个(样式)复选框,它会自动切换,其他所有人都不会受到影响。 Thx非常需要帮助。CSS:复选框切换效果仅适用于第一个复选框

https://jsfiddle.net/99asehku/

HTML:

<div class="onoffswitch"> 
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked> 
    <label class="onoffswitch-label" for="myonoffswitch"> 
     <span class="onoffswitch-inner"></span> 
     <span class="onoffswitch-switch"></span> 
    </label> 
</div><br><br> 
<div class="onoffswitch"> 
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked> 
    <label class="onoffswitch-label" for="myonoffswitch"> 
     <span class="onoffswitch-inner"></span> 
     <span class="onoffswitch-switch"></span> 
    </label> 
</div><br><br> 
<div class="onoffswitch"> 
    <input type="checkbox" name="onoffswitch1" class="onoffswitch-checkbox1" id="myonoffswitch1" checked> 
    <label class="onoffswitch-label1" for="myonoffswitch1"> 
     <span class="onoffswitch-inner1"></span> 
     <span class="onoffswitch-switch1"></span> 
    </label> 
</div> 

CSS:

所有ID的
.onoffswitch { 
    position: relative; width: 58px; 
    -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 #999999; border-radius: 20px; 
} 
.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: 10px; padding: 0; line-height: 10px; 
    font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold; 
    box-sizing: border-box; 
} 
.onoffswitch-inner:before { 
    content: "ANO"; 
    padding-left: 10px; 
    background-color: #34A7C1; color: #FFFFFF; 
} 
.onoffswitch-inner:after { 
    content: "NE"; 
    padding-right: 10px; 
    background-color: #EEEEEE; color: #999999; 
    text-align: right; 
} 
.onoffswitch-switch { 
    display: block; width: 10px; margin: 0px; 
    background: #FFFFFF; 
    position: absolute; top: 0; bottom: 0; 
    right: 44px; 
    border: 2px solid #999999; border-radius: 20px; 
    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; 
} 
+0

我已经自由地评论了你的JSFiddle链接,因为你故意绕过堆栈溢出的*** JSFiddle链接必须伴随代码***警告通过将链接放置在代码块中。我还投票决定关闭这个问题,因为你在问题本身没有提供任何代码。 –

+0

对不起,我没有为自己翻译这个错误信息,我迫切需要得到解决这个问题的任何提示。我很高兴看到消除这种负面声誉。 Thx –

+0

好得多。我收回了我的近距离投票。 –

回答

-1

首先必须是唯一的。不要为相同的元素使用相同的ID。

并使for为所有不同。

.onoffswitch { 
 
    position: relative; width: 58px; 
 
    -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 #999999; border-radius: 20px; 
 
} 
 
.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: 10px; padding: 0; line-height: 10px; 
 
    font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold; 
 
    box-sizing: border-box; 
 
} 
 
.onoffswitch-inner:before { 
 
    content: "ANO"; 
 
    padding-left: 10px; 
 
    background-color: #34A7C1; color: #FFFFFF; 
 
} 
 
.onoffswitch-inner:after { 
 
    content: "NE"; 
 
    padding-right: 10px; 
 
    background-color: #EEEEEE; color: #999999; 
 
    text-align: right; 
 
} 
 
.onoffswitch-switch { 
 
    display: block; width: 10px; margin: 0px; 
 
    background: #FFFFFF; 
 
    position: absolute; top: 0; bottom: 0; 
 
    right: 44px; 
 
    border: 2px solid #999999; border-radius: 20px; 
 
    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; 
 
}
<div class="onoffswitch"> 
 
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked> 
 
    <label class="onoffswitch-label" for="myonoffswitch"> 
 
     <span class="onoffswitch-inner"></span> 
 
     <span class="onoffswitch-switch"></span> 
 
    </label> 
 
</div><br><br> 
 
<div class="onoffswitch"> 
 
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch1" checked> 
 
    <label class="onoffswitch-label" for="myonoffswitch1"> 
 
     <span class="onoffswitch-inner"></span> 
 
     <span class="onoffswitch-switch"></span> 
 
    </label> 
 
</div><br><br> 
 
<div class="onoffswitch"> 
 
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch2" checked> 
 
    <label class="onoffswitch-label" for="myonoffswitch2"> 
 
     <span class="onoffswitch-inner"></span> 
 
     <span class="onoffswitch-switch"></span> 
 
    </label> 
 
</div>

+0

Thx ketan我会尝试 –

+0

@FilipCZ乐意提供帮助。你只能接受一个答案。不是多个。 – ketan

-1

你必须给每个复选框交换机的名称和ID唯一。

看这个:jsfiddle;最后一个有自己的ID和名字,而第二个有相同的ID和第一个名字。 (当然,CSS代码必须被重复或修改规则,以适用于每个新的ID)

<div class="onoffswitch"> 
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked> 
<label class="onoffswitch-label" for="myonoffswitch"> 
    <span class="onoffswitch-inner"></span> 
    <span class="onoffswitch-switch"></span> 
</label> 





+0

完美的silviagreen,我也很高兴你帮助了我。 –

+0

为什么downvote? – silviagreen