如何为proto.io的开关分配事件处理程序?

3
Proto.io有一个很好的开关解决方案,非常易于使用。然而,我遇到了麻烦,不知道如何在点击之后向开关添加事件处理程序。具体来说,我想知道如何确定在我点击之后开关所处的状态。
我尝试检查元素复选框的状态,但在proto.io中似乎没有变化。
HTML和CSS如下:

.onoffswitch {
    position: relative; width: 90px;
    -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: 30px; padding: 0; line-height: 30px;
    font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
    box-sizing: border-box;
}
.onoffswitch-inner:before {
    content: "ON";
    padding-left: 10px;
    background-color: #34A7C1; color: #FFFFFF;
}
.onoffswitch-inner:after {
    content: "OFF";
    padding-right: 10px;
    background-color: #EEEEEE; color: #999999;
    text-align: right;
}
.onoffswitch-switch {
    display: block; width: 18px; margin: 6px;
    background: #FFFFFF;
    position: absolute; top: 0; bottom: 0;
    right: 56px;
    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>

谢谢!

3个回答

1

I handled the on / off change event in this way:

$("#myonoffswitch").change(function () {
    // do some stuff
});

然后,为了获取开关的状态,我使用了以下代码:

if ($("#myonoffswitch").is(":checked"))
     // on
else
     // off

0
在复选框中添加一个 onclick 事件,并检查 this.checked
<input type="checkbox" name="onoffswitch" 
class="onoffswitch-checkbox" id="myonoffswitch" checked
onclick="alert(this.checked);">

0

EstevaoLuis的答案很好用,因为我最近不管是使用还是不使用jQuery都需要用到它,所以这里提供一些可能会对你有用的代码片段:

使用jQuery:

$(document).ready(function() {
  var onoffswitch1 = $("#myonoffswitch");
  onoffswitch1.on("change", function() {
    if (onoffswitch1.is(":checked")) console.log("1-ON");
    else console.log("1-OFF");
  });
});

没有jQuery:

var onoffswitch2 = document.querySelector('#myonoffswitch');
onoffswitch2.addEventListener("change", function(e) {
  if (this.checked) console.log("2-ON");
  else console.log("2-OFF");
}, false);

Demo:

$(document).ready(function() {
  var onoffswitch1 = $("#myonoffswitch");
  onoffswitch1.on("change", function() {
    if (onoffswitch1.is(":checked")) console.log("1-ON");
    else console.log("1-OFF");
  });
});

var onoffswitch2 = document.querySelector('#myonoffswitch');
onoffswitch2.addEventListener("change", function(e) {
  if (this.checked) console.log("2-ON");
  else console.log("2-OFF");
}, false);
.onoffswitch {
  position: relative;
  width: 90px;
  -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: 30px;
  padding: 0;
  line-height: 30px;
  font-size: 14px;
  color: white;
  font-family: Trebuchet, Arial, sans-serif;
  font-weight: bold;
  box-sizing: border-box;
}

.onoffswitch-inner:before {
  content: "ON";
  padding-left: 10px;
  background-color: #34A7C1;
  color: #FFFFFF;
}

.onoffswitch-inner:after {
  content: "OFF";
  padding-right: 10px;
  background-color: #EEEEEE;
  color: #999999;
  text-align: right;
}

.onoffswitch-switch {
  display: block;
  width: 18px;
  margin: 6px;
  background: #FFFFFF;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 56px;
  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;
}
<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" checked>
  <label class="onoffswitch-label" for="myonoffswitch">
    <span class="onoffswitch-inner"></span>
    <span class="onoffswitch-switch"></span>
  </label>
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接