代码之家  ›  专栏  ›  技术社区  ›  Mikey1980

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

  •  1
  • Mikey1980  · 技术社区  · 15 年前

    我试图根据一些单选按钮的值隐藏或显示一个div,css默认div为display='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 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';
        }
    }
    

    4 回复  |  直到 15 年前
        1
  •  1
  •   Chris    15 年前

    我认为它可能会说“getElementById”没有定义的唯一原因是,如果您在原始代码中输入了一个在将示例放在这里时没有输入的错误(区分大小写总是让我明白的),或者您在页面的其他地方重新定义了文档。

        2
  •  0
  •   nsmyself    15 年前

    嗯,你确定你的身份证是对的吗? 检查重复项。如果看不到实际的代码很难判断。

        3
  •  0
  •   Shaded    15 年前

    这里一切都很稳固。如果您得到的getElementById()没有定义,那么它可能是一个输入错误或者缺少一些语法,比如不匹配的括号。你可以假设你的html现在是正确的,集中在脚本标签或文件中的函数是定义的,我很肯定你的问题就在那里。

        4
  •  0
  •   peelman    15 年前

    你那里的代码,一行一行,在Safari中对我有用。getElementByID类似于DOM根实现的一部分;如果它没有定义,我想你有问题的火狐。

    http://firefiter.com/temp.php