javascript - 通过单选按钮隐藏/显示 div

标签 javascript html onchange

我试图根据某些单选按钮的值隐藏或显示 div,css 默认 div 显示='none'

这里是输入

<input type="radio" id="payment_type" value="cash" checked="checked" name="payment_type" onchange="showhideFinancing(this.value);"/>&nbsp;Cash/Debit
<input type="radio" id="payment_type" value="cheque" name="payment_type" onchange="showhideFinancing(this.value);"/>&nbsp;Cheque
<input type="radio" id="payment_type" value="visa" checked="checked" name="payment_type" onchange="showhideFinancing(this.value);"/>&nbsp;VISA
<input type="radio" id="payment_type" value="mc" name="payment_type" onchange="showhideFinancing(this.value);"/>&nbsp;Mastercard
<input type="radio" id="payment_type" value="financing" name="payment_type" onchange="showhideFinancing(this.value);"/>&nbsp;Financing

这是 div:

<div id="financing">
...
</div>

我的 Javascript fn 看起来像这样:

function showhideFinancing(payment_type) {
    if (payment_type == "financing") {
        document.getElementById("financing").style.display = 'block';
    } else {
        document.getElementById("financing").style.display = 'none';
    }
}

Firebug 显示 getElementById 未定义 - 谢谢!

最佳答案

我认为它可能会说“getElementById”未定义的唯一原因是,如果您在原始代码中犯了一个拼写错误,但在将示例放在这里时却没有这样做(区分大小写始终是明白我的意思)或者如果您以某种方式在页面的其他位置重新定义了文档。

关于javascript - 通过单选按钮隐藏/显示 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3070780/

相关文章:

javascript - 自定义验证器无法访问 Angular 6 Reactive 表单中的表单

javascript - 使用 IntelliJ 和源映射启用 JavaScript 调试

javascript - 类型错误 : button is null but working fine in console

html - 创建响应式图像行

javascript - Electron 中的图像文件上传和检索

javascript - 在 ios react-native 的后台任务中进行倒计时

html - 奇怪的填充/边距显示在 firefox/chrome

Angular, typescript 变化检测输入文本

javascript - 是否可以对 javascript 变量进行更改事件?

javascript - 为什么在使用 .focus() 时我的 onchange 函数被调用了两次?