同样,我不擅长编码(我没有经验)。
但是,在这个例子中,我想获得一个Toggle Switches列表的状态。
我设法制作了开关列表。我怎样才能了解它们的状态。
更准确地说,我需要以逗号分隔的Checked项目列表,如Button 1,Button 2,Button 5(当选中Button 1,Button 2和Button 5时)。
<!DOCTYPE html>
<html>
<head>
<style>
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {display:none;}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
</style>
</head>
<body>
<h2>
<center>Smartwatch Tasks Toggle</center>
</h2>
<table style="width:100%">
<tr>
<td>Switch 1</td>
<td><label class="switch">
<id name="Switch 1">
<input type="checkbox" unchecked>
<span class="slider round"></span>
</label></td>
</tr>
<tr>
<td>Switch 2</td>
<td><label class="switch">
<id name="Switch 2">
<input type="checkbox" unchecked>
<span class="slider round"></span>
</label></td>
</tr>
<tr>
<td>Switch 3</td>
<td><label class="switch">
<id name="Switch 3">
<input type="checkbox" unchecked>
<span class="slider round"></span>
</label></td>
</tr>
<tr>
<td>Switch 4</td>
<td><label class="switch">
<id name="Switch 4">
<input type="checkbox" unchecked>
<span class="slider round"></span>
</label></td>
</tr>
<tr>
<td>Switch 5</td>
<td><label class="switch">
<id name="Switch 5">
<input type="checkbox" unchecked>
<span class="slider round"></span>
</label></td>
</tr>
</table>
<br><br>
<button type="button" onclick="alert('Hello world!')">Save</button>
<br><br>
<button type="button" onclick="alert('Hello world!')">Cancel</button>
</body>
</html>
答案 0 :(得分:1)
Try this:
Change in html code
<table style="width:100%">
<tr>
<td>Switch 1</td>
<td><label class="switch">
<id name="Switch 1">
<input type="checkbox" class="testClass" value="1" unchecked>
<span class="slider round"></span>
</label></td>
</tr>
<tr>
<td>Switch 2</td>
<td><label class="switch">
<id name="Switch 2">
<input type="checkbox" class="testClass" value="2" unchecked>
<span class="slider round"></span>
</label></td>
</tr>
<tr>
<td>Switch 3</td>
<td><label class="switch">
<id name="Switch 3">
<input type="checkbox" class="testClass" value="3" unchecked>
<span class="slider round"></span>
</label></td>
</tr>
<tr>
<td>Switch 4</td>
<td><label class="switch">
<id name="Switch 4">
<input type="checkbox" class="testClass" value="4" unchecked>
<span class="slider round"></span>
</label></td>
</tr>
<tr>
<td>Switch 5</td>
<td><label class="switch">
<id name="Switch 5">
<input type="checkbox" class="testClass" value="5" unchecked>
<span class="slider round"></span>
</label></td>
</tr>
</table>
Jquery Code:
var checkedVals = $('.testClass:checkbox:checked').map(function() {
return this.value;
}).get();
alert(checkedVals.join(","));
答案 1 :(得分:1)
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("input").change(function(){
// alert( $('input:checkbox').attr('id'))
alert( $(this).attr('id'))
// $("#selectedVal").val($(this).attr('id'))
});
});
</script>
<style>
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {display:none;}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
</style>
</head>
<body>
<h2>
<center>Smartwatch Tasks Toggle</center>
</h2>
<table style="width:100%">
<tr>
<td>Switch 1</td>
<td><label class="switch">
<id name="Switch 1">
<input type="checkbox" id="switch1" unchecked>
<span class="slider round"></span>
</label></td>
</tr>
<tr>
<td>Switch 2</td>
<td><label class="switch">
<id name="Switch 2">
<input type="checkbox" id="switch2" unchecked>
<span class="slider round"></span>
</label></td>
</tr>
<tr>
<td>Switch 3</td>
<td><label class="switch">
<id name="Switch 3">
<input type="checkbox" id="switch3" unchecked>
<span class="slider round"></span>
</label></td>
</tr>
<tr>
<td>Switch 4</td>
<td><label class="switch">
<id name="Switch 4">
<input type="checkbox" id="switch4" unchecked>
<span class="slider round"></span>
</label></td>
</tr>
<tr>
<td>Switch 5</td>
<td><label class="switch">
<id name="Switch 5">
<input type="checkbox" id="switch5" unchecked>
<span class="slider round"></span>
</label></td>
</tr>
</table>
<br><br>
<button type="button" onclick="alert('Hello world!')">Save</button>
<br><br>
<button type="button" onclick="alert('Hello world!')">Cancel</button>
</body>
</html>
&#13;
我已经使用过Jquery并且在切换时你将获得警报中的值。这是你问的吗?
答案 2 :(得分:1)
您可以在JavaScript中执行此操作。
首先,我建议您将id
(即"Switch 1"
,"Switch 2"
,"Switch 3"
等)分配给每个输入,以便您可以登录它们。
然后,您可以为按钮指定一个功能,以显示已检查的输入。
<强> HTML 强>
<button type="button" onclick="showCheckedInputs()">Show Checked Switches</button>
<强>的JavaScript 强>
function showCheckedInputs() {
var checkboxes = document.querySelectorAll('input[type="checkbox"]');
var checkedInputs = [];
for (var i = 0; i < checkboxes.length; i++) {
if(checkboxes[i].checked) {
checkedInputs.push(checkboxes[i].id)
}
}
// log results to the console
console.log(checkedInputs);
// render results to the DOM
document.getElementById('result').innerHTML = checkedInputs.join(', ');
}
这是Demo
P.S。你的开关看起来很酷!