【发布时间】:2018-07-10 18:48:31
【问题描述】:
如果选中了两个复选框,我会尝试更改按钮的颜色。但我无法弄清楚如何做到这一点。我知道我需要 javascript,但完全没有使用 javascript 的经验。 我从互联网上复制了一些行,但这不会改变按钮的颜色。因此,如果选中复选框,则应该为按钮选择另一种样式。
这是我目前的代码:
<!DOCTYPE html>
<html>
<head>
<style>
.button1 {
background-color: #FF0000;
border: 4px;
border-radius: 20px 20px 20px 20px;
border-color: red;
color: yellow;
padding: 15px 25px;
text-align: center;
font-size: 16px;
cursor: pointer;
}
.button1:hover {
background-color: green;
}
.button2 {
background-color: #FF00FF;
}
</style>
<script type="text/javascript">
function checkbox_checked {
if (input.getAttribute('type') == 'checkbox' && input.checked)
n++;
checkbox_checked.elements.boxes_checked.value = n;
if n = 2 button.className = "button2";
}
</script>
</head>
<body style="background-color:#E3CEF6;">
<button class="button1">General</button>
<!-- Insert a table in a div, so this can be hide -->
<div id="General">
<table style="width:50%;margin-left:50px;">
<colgroup>
<col span="3" style="background-color:#E3CEF6;">
<col style="background-color:yellow">
</colgroup>
<tr>
<td><label class="container"> <input type="checkbox" name="cb_General_1"> <span class="checkmark"></span> </label> </td>
<td>Protocol name(s) : </td>
<td><input type="text" name="Protocol name(s)" size="35"> </td>
</tr>
<tr>
<td><label class="container"> <input type="checkbox" name="cb_General_2"> <span class="checkmark"></span> </label></td>
<td>Order name(s):</td>
<td><input type="text" name="Order name(s)" size="35"></td>
</tr>
</table>
</div>
</body>
</html>
【问题讨论】:
-
函数在你调用它们之前不会做任何事情。你最好先添加事件监听器
-
之前在 stackoverflow 上已回答:stackoverflow.com/questions/31705665/…
标签: javascript html