我尝试快速详细地解释我打算做什么。 我有一个表,每个单元格都放置一个按钮CSS,如下所示:
<input type="button" class="AreaFree" id="#"> //a different id for each button
我的目的是选择(点击)许多不同的按钮;每次选择一个按钮时,它必须改变颜色(或改变按钮的类别);如果再次按下,它必须返回其原始状态。 接下来,我想要一个提交按钮,该按钮调用一个servlet并发送哪些按钮已被选中,哪些按钮未被选中。 现在我的问题:是否可以使用JavaScript?如果是,您能否分享执行此操作所需的代码?如果不是,你有什么建议?
现在我分享了一段为此目的而涉及的代码。如果我不是很详细的话,我很抱歉。
HTML
<form action="myServlet" method="post">
<table cellspacing="0" cellpadding="0">
<tr>
<td><input type="button" class="AreaFree" id="11"/></td>
<td><input type="button" class="AreaFree" id="12"/></td>
<td><input type="button" class="AreaFree" id="13"/></td>
<td><input type="button" class="AreaFree" id="14"/></td>
<td><input type="button" class="AreaFree" id="15"/></td>
</tr>
<tr>
<td><input type="button" class="AreaFree" id="21"/></td>
<td><input type="button" class="AreaFree" id="22"/></td>
<td><input type="button" class="AreaFree" id="23"/></td>
<td><input type="button" class="AreaFree" id="24"/></td>
<td><input type="button" class="AreaFree" id="25"/></td>
</tr>
</table>
<input type="submit" value="submit">
</form>
CSS
.AreaFree{
-moz-box-shadow:inset 0px 1px 0px 0px #3dc21b;
-webkit-box-shadow:inset 0px 1px 0px 0px #3dc21b;
background-color:#44c767;
border:1px solid #18ab29;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-family:Arial;
font-size:16px;
font-weight:bold;
padding:15px 21px;
text-decoration:none;
}
.AreaFree:hover {
background-color:#5cbf2a;
}
.AreaFree:active {
position:relative;
}
.AreaOccupated{
-moz-box-shadow:inset 0px 1px 0px 0px #3dc21b;
-webkit-box-shadow:inset 0px 1px 0px 0px #3dc21b;
background-color:#E00000;
border:1px solid #B00000;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-family:Arial;
font-size:16px;
font-weight:bold;
padding:15px 21px;
text-decoration:none;
}
.AreaOccupated:hover {
background-color:#D00000;
}
.AreaOccupated:active {
position:relative;
}
.AreaBlocked{
-moz-box-shadow:inset 0px 1px 0px 0px #3dc21b;
-webkit-box-shadow:inset 0px 1px 0px 0px #3dc21b;
background-color:#A8A8A8;
border:1px solid #808080;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-family:Arial;
font-size:16px;
font-weight:bold;
padding:15px 21px;
text-decoration:none;
}
.AreaBlocked:hover {
background-color:#989898;
}
.AreaBlocked:active {
position:relative;
}
答案 0 :(得分:1)
将此添加到html的底部:
$('input[type="button"]').on('click', function(evt) {
var ary = $(this).attr('class').split(' ');
if (ary.indexOf('clicked') === -1) {
$(this).addClass('clicked');
}
else {
$(this).removeClass('clicked');
}
});
同时添加此课程:
input.clicked {
background-color: red;
}
现在,您必须处理悬停颜色问题,但代码应该为您提供良好的开端。
更新:(此代码适用于我)
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.AreaFree{
-moz-box-shadow:inset 0px 1px 0px 0px #3dc21b;
-webkit-box-shadow:inset 0px 1px 0px 0px #3dc21b;
background-color:#44c767;
border:1px solid #18ab29;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-family:Arial;
font-size:16px;
font-weight:bold;
padding:15px 21px;
text-decoration:none;
}
.AreaFree:hover {
background-color:#5cbf2a;
}
.AreaFree:active {
position:relative;
}
.AreaOccupated{
-moz-box-shadow:inset 0px 1px 0px 0px #3dc21b;
-webkit-box-shadow:inset 0px 1px 0px 0px #3dc21b;
background-color:#E00000;
border:1px solid #B00000;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-family:Arial;
font-size:16px;
font-weight:bold;
padding:15px 21px;
text-decoration:none;
}
.AreaOccupated:hover {
background-color:#D00000;
}
.AreaOccupated:active {
position:relative;
}
.AreaBlocked{
-moz-box-shadow:inset 0px 1px 0px 0px #3dc21b;
-webkit-box-shadow:inset 0px 1px 0px 0px #3dc21b;
background-color:#A8A8A8;
border:1px solid #808080;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-family:Arial;
font-size:16px;
font-weight:bold;
padding:15px 21px;
text-decoration:none;
}
.AreaBlocked:hover {
background-color:#989898;
}
.AreaBlocked:active {
position:relative;
}
input.clicked {
background-color: red;
}
</style>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
</head>
<body>
<form action="myServlet" method="post">
<table cellspacing="0" cellpadding="0">
<tr>
<td><input type="button" class="AreaFree" id="11"/></td>
<td><input type="button" class="AreaFree" id="12"/></td>
<td><input type="button" class="AreaFree" id="13"/></td>
<td><input type="button" class="AreaFree" id="14"/></td>
<td><input type="button" class="AreaFree" id="15"/></td>
</tr>
<tr>
<td><input type="button" class="AreaFree" id="21"/></td>
<td><input type="button" class="AreaFree" id="22"/></td>
<td><input type="button" class="AreaFree" id="23"/></td>
<td><input type="button" class="AreaFree" id="24"/></td>
<td><input type="button" class="AreaFree" id="25"/></td>
</tr>
<input type="submit" value="submit">
</form>
<script>
$('input[type="button"]').on('click', function(evt) {
var ary = $(this).attr('class').split(' ');
if (ary.indexOf('clicked') === -1) {
$(this).addClass('clicked');
}
else {
$(this).removeClass('clicked');
}
});
</script>
</body>
</html>
更新:
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.AreaFree {
-moz-box-shadow: inset 0px 1px 0px 0px #3dc21b;
-webkit-box-shadow: inset 0px 1px 0px 0px #3dc21b;
background-color: #44c767;
border: 1px solid #18ab29;
display: inline-block;
cursor: pointer;
color: #ffffff;
font-family: Arial;
font-size: 16px;
font-weight: bold;
padding: 15px 21px;
text-decoration: none;
}
.AreaFree:hover {
background-color: #5cbf2a;
}
.AreaFree:active {
position: relative;
}
.AreaOccupated {
-moz-box-shadow: inset 0px 1px 0px 0px #3dc21b;
-webkit-box-shadow: inset 0px 1px 0px 0px #3dc21b;
background-color: #E00000;
border: 1px solid #B00000;
display: inline-block;
cursor: pointer;
color: #ffffff;
font-family: Arial;
font-size: 16px;
font-weight: bold;
padding: 15px 21px;
text-decoration: none;
}
.AreaOccupated:hover {
background-color: #D00000;
}
.AreaOccupated:active {
position: relative;
}
.AreaBlocked {
-moz-box-shadow: inset 0px 1px 0px 0px #3dc21b;
-webkit-box-shadow: inset 0px 1px 0px 0px #3dc21b;
background-color: #A8A8A8;
border: 1px solid #808080;
display: inline-block;
cursor: pointer;
color: #ffffff;
font-family: Arial;
font-size: 16px;
font-weight: bold;
padding: 15px 21px;
text-decoration: none;
}
.AreaBlocked:hover {
background-color: #989898;
}
.AreaBlocked:active {
position: relative;
}
input.clicked {
background-color: red;
}
</style>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
</head>
<body>
<form action="myServlet" method="post">
<table cellspacing="0" cellpadding="0">
<tr>
<td>
<input type="button" class="AreaFree" id="0" />
</td>
<td>
<input type="button" class="AreaFree" id="1" />
</td>
<td>
<input type="button" class="AreaFree" id="2" />
</td>
<td>
<input type="button" class="AreaFree" id="3" />
</td>
<td>
<input type="button" class="AreaFree" id="4" />
</td>
</tr>
<tr>
<td>
<input type="button" class="AreaFree" id="5" />
</td>
<td>
<input type="button" class="AreaFree" id="6" />
</td>
<td>
<input type="button" class="AreaFree" id="7" />
</td>
<td>
<input type="button" class="AreaFree" id="8" />
</td>
<td>
<input type="button" class="AreaFree" id="9" />
</td>
</tr>
<input type="hidden" id="matrix" value="" />
<input type="submit" value="submit">
</form>
<script>
var matrix = [];
for (var i = 0; i < 10; i++) {
matrix.push(0);
}
// set the hidden field on init
$('#matrix').val(matrix);
$('input[type="button"]').on('click', function(evt) {
var me = $(this);
var idx = +me.attr('id'); // the + sign turns this value to a number
if (matrix[idx] === 0) {
matrix[idx] = 1;
me.addClass('clicked');
} else {
matrix[idx] = 0;
me.removeClass('clicked');
}
// update the hidden field every time a user clicks something
$('#matrix').val(matrix);
});
</script>
</body>
</html>
我在最新的代码中做了一些事情。由于我们在矩阵数组中跟踪状态,因此无需查看类。使用矩阵中的状态来确定。比较0和1比字符串和数组快。
我还更改了按钮ID以帮助更新矩阵。