Proto.io有一个很好的开/关开关解决方案,它们非常易于使用。但是,我在查看单击后如何向交换机添加事件处理程序时遇到问题。具体来说,我在询问后如何确定开关所处的状态。
我已经尝试检查<input>
元素复选框的状态,但似乎没有使用proto.io进行更改。
以下是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>
这里是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;
}
谢谢!
答案 0 :(得分:0)
在复选框中添加onclick
个事件,然后检查this.checked
。
<input type="checkbox" name="onoffswitch"
class="onoffswitch-checkbox" id="myonoffswitch" checked
onclick="alert(this.checked);">
答案 1 :(得分:0)
我以这种方式处理了开/关更改事件:
$("#myonoffswitch").change(function () {
// do some stuff
});
然后,使用以下代码获取开关状态:
if ($("#myonoffswitch").is(":checked"))
// on
else
// off
答案 2 :(得分:0)
EstevaoLuis的答案很好用,因为我最近不得不在有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);
没有jQuery:
$(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>
struct ContentView: View {
@State private var pathURL = "/Users/cesare/Desktop/Test"
var body: some View {
VStack {
TextField("/Users/cesare/Desktop/Test", text: $pathURL)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
Button(action: {
let fileUrl = URL(fileURLWithPath: self.pathURL, isDirectory: true)
let nameImage = "ImageTest" + ".jpg"
let fileUrlWithName = fileUrl.appendingPathComponent(nameImage)
let imageData = UIImage(named: "2020-03-05_11-19-22_5")?.jpegData(compressionQuality: 1)
do {
try imageData!.write(to: fileUrlWithName)
} catch {
print("** saveImageData error: \(error.localizedDescription)")
}
}) {
Text("Save Image")
}
}
}
}