【问题标题】:In an HTML form, can I have a button only be visible/selectable based on the value of a checkbox?在 HTML 表单中,我可以让按钮仅根据复选框的值可见/可选择吗?
【发布时间】:2011-04-09 20:01:52
【问题描述】:
我正在尝试为我公司的网站创建一个表单。我让客户在下载软件后有选择地向我的公司捐款。如果他们选中一个框,我希望出现一个带有代码的“捐赠”按钮去我的第三方商店(我知道怎么做),如果他们没有选中复选框,那么我想要将出现一个不同的按钮,将它们链接到软件的下载(我也可以这样做)。
我更愿意坚持使用 PHP、Javascript 和 HTML。我无法立即访问像 asp.net 这样的东西。
谢谢!
【问题讨论】:
标签:
java
javascript
html
webforms
checkbox
【解决方案1】:
是的。只需使用 JavaScript 在 'none' 和 '' 之间切换 button.style.display 的值。
【解决方案2】:
<input id="my_check_box" type="checkbox" onclick="handleCheckChange()" onchange="handleCheckChange()" />
<input id="my_donate_button" type="submit" style="display: none" value="Donate now!" />
<script type="text/javascript">
function handleCheckChange() {
var my_donate_button = document.getElementById("my_donate_button");
if (document.getElementById("my_check_box").checked) {
my_donate_button.style.display = '';
} else {
my_donate_button.style.display = 'none';
}
}
// Browsers may pre-check the box if the user re-loads the page,
// so call this to make sure the page is consistent.
handleCheckChange();
</script>
【解决方案3】:
你可以的..
创建一个名为
的 javascript 函数
函数 check_button()
{
if(document.getElementById('checkbox_id').value=="checkbox_value")
{
document.getElementById('button_id').style.display="block";
}
别的
{
document.getElementById('button_id').style.display="none";
}
}
在复选框 html 代码中调用此函数